Home >Web Front-end >JS Tutorial >The difference between substring, substr and splice in js
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 JavaScript
The substring, substr and splice functions in JavaScript are all used for manipulating strings, but they have different uses and uses.
substring
substring(start, end)
Parameters:
substr
substr(start, length)
Parameters:
splice
splice(start, deleteCount, ...items)
Parameters:
Usage comparison
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>
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!