假设有字符串如下:
var str = " this is a test string! ";
这个字符串前后都有空格,如果我们想要只截取this is a string!而去掉首尾的空格,
我们可以对该字符串进行如下操作:
str = str.substring(1,str.substring.length - 1);
如此一来,我们就能跳过首尾的空格而截取完整的字符串;
该操作的语法为: str = str.substring(firstindex,lastindex);
理论上,我们可以通过这个方法截取知道首尾位置的字符串的任意子字符串。