Home > Web Front-end > JS Tutorial > The difference between substring, substr and splice in js

The difference between substring, substr and splice in js

下次还敢
Release: 2024-05-06 10:36:17
Original
747 people have browsed it

JavaScript functions for operating strings include: substring extracts substrings within a specified range; substr extracts substrings with a specified starting point and length (end can exceed the length); splice can delete and/or replace parts of a string The specified number of characters, and replacement characters can be inserted.

The difference between substring, substr and splice in js

The difference between substring, substr and splice in JavaScript

The substring, substr and splice functions in JavaScript are all used for manipulating strings, but they have different uses and uses.

substring

  • Extract the specified range of parts of the string.
  • Syntax: substring(start, end)
  • Parameters:

    • start: The character index to start extracting (from 0 starts).
    • end: Index of the last character to extract (exclusive).

substr

  • Similar to substring, but when end exceeds the length of the string, substr will truncate it to String length.
  • Syntax: substr(start, length)
  • Parameters:

    • start: The character index to start extracting (from 0 starts).
    • length: The number of characters to extract.

splice

  • Variadic function for removing and/or replacing characters in a string.
  • Syntax: splice(start, deleteCount, ...items)
  • Parameters:

    • start: To start deletion and the replacement character index (starting from 0).
    • deleteCount: The number of characters to be deleted.
    • items (optional): New characters or strings to be inserted at the positions of deleted characters.

Usage comparison

  • Extract substring: Use substring or substr, but substr More convenient when end exceeds the length of the string.
  • Delete characters: Use splice, specify deleteCount as the number of characters to be deleted.
  • Replacement characters: Use splice and provide replacement characters or strings.

Example

<code class="javascript">const str = "Hello World";

// 使用 substring 提取 "World"
console.log(str.substring(6)); // "World"

// 使用 substr 提取 "World"(即使 end 超出范围)
console.log(str.substr(6)); // "World"

// 使用 splice 删除 "Hello"
str.splice(0, 5);
console.log(str); // "World"

// 使用 splice 替换 "World" 为 "JavaScript"
str.splice(0, 5, "JavaScript");
console.log(str); // "JavaScript"</code>
Copy after login

The above is the detailed content of The difference between substring, substr and splice in js. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template