[英]remove all characters of a string after 5th character?
I need to remove all characters of a string after 5th character, for example 例如,我需要删除字符串中第5个字符后的所有字符
my string is 我的绳子是
P-CI-7-C-71
and the output should be like 和输出应该像
P-CI-
alert("P-CI-7-C-71".substring(0, 5));
So you can use it like 所以你可以像这样使用它
var str='P-CI-7-C-71'.substring(0, 5);
You can also use substr , that will work the same in this case but remember they work differently so they are not generally interchangeable 您还可以使用substr ,在这种情况下,它们的工作原理相同,但请记住它们的工作原理不同,因此通常不可互换
var str='P-CI-7-C-71'.substr(0, 5);
Difference can be noticed in the prototype 原型中可以发现差异
str.substring(indexStart[, indexEnd])
str.substring(indexStart [,indexEnd])
VS VS
str.substr(start[, length])
str.substr(开始[,长度])
Extracting String Parts 提取弦乐部分
There are 3 methods for extracting a part of a string: 有三种方法可以提取字符串的一部分:
slice(start, end)
substring(start, end)
substr(start, length)
With String#substring
: 使用
String#substring
:
The
substring()
method returns a subset of a string between one index and another, or through the end of the string.substring()
方法返回一个索引与另一个索引之间或字符串末尾的字符串子集。
document.write('P-CI-7-C-71'.substring(0, 5));
Try this: 尝试这个:
alert('P-CI-7-C-71'.substring(0,5));
var str = 'P-CI-7-C-71'; var res = str.slice(0,5); alert(res);
Use Can use substring function in the JS 使用可以在JS中使用子字符串功能
the Substring(StringName,StartPosition, End Position);
Substring(StringName,StartPosition,End Position);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.