Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS中对字符串截取的方法 #17

Open
Pieces312 opened this issue Sep 29, 2020 · 0 comments
Open

JS中对字符串截取的方法 #17

Pieces312 opened this issue Sep 29, 2020 · 0 comments

Comments

@Pieces312
Copy link
Owner

Pieces312 commented Sep 29, 2020

字符串截取方法:

  • substr(index, length):index是指开始截取的位置,如果为负数则开始截取的位置为index+字符串的长度;length表示需要截取的字符串的长度,如果给定的length长度大于整个字符串的长度,就会以字符串的长度为准;

  • substring(first, last): first表示开始截取的位置,如果为负数开始截取的位置会从0开始;last表示截取结束的位置,如果last<first,那么两者调换位置再进行截取;

  • slice(first, last):first表示开始截取的位置,如果为负数则开始截取的位置为index+字符串的长度;last表示截取结束的位置,如果为负数截取结束的位置为last+字符串的长度。但是要注意的是first必须大于last,否则会返回空,就算为负数也必须是(first+字符串长度)> (last+字符串长度)。也就是说无论为正数还是负数,first都必须大于last。

文章链接:https://juejin.im/post/6844903818015014920

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant