详解字符串方法及实例
- charAt()
str.charAt(index)
var str="hello world";
var str1=str.charAt(6);
console.log(str1);
- charCodeAt()
str.charCodeAt(index)
var str="hello world";
var str1=str.charCodeAt(1);
var str2=str.charCodeAt(-2);
console.log(str1);
- toUpperCase().toLowerCase()
var str="Hello World";
var str1=str.toLowerCase();
console.log(str);
console.log(str1);
var str="hello world";
var str1=str.toUpperCase();
console.log(str);
console.log(str1);
- indexOf()
str.indexOf(searchStr,startIndex)
var str="Hello World";
var str1=str.indexOf("o");
var str2=str.indexOf("world");
var str3=str.indexOf("o",str1+1);
console.log(str1);
console.log(str2);
console.log(str3);
- replace()
str.replace(regexp/substrOld,replaceStrNew)
var reg=/o/ig;
var myString = "Hello kitty";
myString.replace(/kitty/,"world")
var name = "Doe, John";
name.replace(/(\w+)\s*, \s*(\w+)/, "$2 $1");
- substring()
var str="Hello World";
var str1=str.substring(2)
var str2=str.substring(2,2);
var str3=str.substring(2,7);
console.log(str1);
console.log(str2);
console.log(str3);