在学习Javascript的过程中,我用到了一些对字符串进行的操作,现在做一个小总结。
一、搜索字符串
搜索字符串,如果返回值为-1,则表示没有要搜索的字符串,如果有要搜索的字符串,则返回字符串所在原字符的位置。
例如:
二、截取字符串
substring 方法将返回一个包含从start 到最后(不包含end)的子字符串的字符串
例如:
一、搜索字符串
var string = 'abcdef';
console.log(string.indexOf('b'));
搜索字符串,如果返回值为-1,则表示没有要搜索的字符串,如果有要搜索的字符串,则返回字符串所在原字符的位置。
例如:
var string='abcdef';
console.log(string.indexOf('b')); // 返回值为1
console.log(string.indexOf('d')); // 返回值为3
二、截取字符串
var string = 'abcdef';
console.log(string.substring(0,1));
console.log(string.substring(3));
substring 方法将返回一个包含从start 到最后(不包含end)的子字符串的字符串
例如:
var string = 'abcdef';
console.log(string.substring(0,1)); // 返回值为a
console.log(string.substring(3)); //返回值为def
console.log(string.substring(2,4)); //返回值为cd
console.log(string.substring(0)); //返回值为abcdef