JavaScript 中的 slice() 和 substr() 有什么区别?

发布于 2024-10-09 14:20:01 字数 79 浏览 0 评论 0原文

请问 JavaScript 中字符串对象 slice()substr() 有什么区别?

Can I ask what the difference is between string object slice() and substr() in JavaScript?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

你没皮卡萌 2024-10-16 14:20:01

它们有不同的签名, .slice () 是:

string.slice(beginIndex, endIndex)

.substr() 是:

string.substr(beginIndex, length);

例如,如果我们有 "1234" 并想要 "23", https://developer.mozilla.org/en-US/

"1234".slice(1,3)
//or...
"1234".substr(1,2)

它们对于很少使用的索引也有不同的行为,请查看MDC文档中的.slice().substr() 了解完整说明。

They have different signatures, .slice() is:

string.slice(beginIndex, endIndex)

Whereas .substr() is:

string.substr(beginIndex, length);

So for example, if we have "1234" and wanted "23", it would be:

"1234".slice(1,3)
//or...
"1234".substr(1,2)

They also have different behavior for the more-rarely used negative indexes, look at the MDC documentation for .slice() and .substr() for full descriptions.

回梦 2024-10-16 14:20:01
  1. String.slice(开始、结束)

    此方法将从 beginend 字符剪切文本,例如:

    alert("Hello World!".slice(1, 8)); // 你好我
    
  2. String.substr(开始, 长度)

    此方法会将文本从 begin 剪切到 begin + length 字符,例如:

    alert("Hello World!".substr(1, 8)); // 你好,沃
    
  1. String.slice(begin, end)

    This method will cut text from begin to end char, eg.:

    alert("Hello World!".slice(1, 8)); // ello Wo
    
  2. String.substr(begin, length)

    This method will cut text from begin to begin + length char, eg.:

    alert("Hello World!".substr(1, 8)); // ello Wor
    
征棹 2024-10-16 14:20:01
var str="Hello world!";
document.write(str.substring(3,7)+"<br />");
document.write(str.slice(3,7)+"<br />");
document.write(str.substr(3,7));

结果:

lo w
lo w
lo worl
var str="Hello world!";
document.write(str.substring(3,7)+"<br />");
document.write(str.slice(3,7)+"<br />");
document.write(str.substr(3,7));

result:

lo w
lo w
lo worl
九局 2024-10-16 14:20:01

Substring()

1.如果start等于stop,则返回空字符串。
2.如果省略stop,则提取字符到字符串末尾。
3.如果开始>停止,然后 substring 将交换这两个参数。
4.如果任一参数大于字符串的长度,则任一参数都将使用字符串的长度。
5.如果任一参数小于 0 或为 NaN,则将其视为 0。

slice()

1.如果 start 等于 stop,则返回一个空字符串,与 substring() 完全相同。
2.如果省略 stop,slice 会提取字符到字符串末尾,与 substring() 完全相同。
3.如果开始>停止,slice() 不会交换 2 个参数。
4.如果任一参数大于字符串的长度,则任一参数都将使用字符串的长度,与 substring() 完全相同。

Substring()

1.If start equals stop, it returns an empty string.
2.If stop is omitted, it extracts characters to the end of the string.
3.If start > stop, then substring will swap those 2 arguments.
4.If either argument is greater than the string's length, either argument will use the string's length.
5.If either argument is less than 0 or is NaN, it is treated as if it were 0.

slice()

1.If start equals stop, it returns an empty string, exactly like substring().
2.If stop is omitted, slice extracts chars to the end of the string, exactly like substring().
3.If start > stop, slice() will NOT swap the 2 arguments.
4.If either argument is greater than the string's length, either argument will use the string's length, exactly like substring().

轻拂→两袖风尘 2024-10-16 14:20:01

我认为 str.slice()str.substr() 之间的区别在于第二个参数:

.slice() 采用 EndIndex 而 < code>.substr() 的长度如下:

.slice(StartIndex,EndIndex).substr(StartIndex,length)。

I think the difference between str.slice() and str.substr() is the second parameter:

.slice() takes EndIndex while .substr() takes length as in:

.slice(StartIndex,EndIndex) and .substr(StartIndex,length).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文