字符方法
1.charAt()方法返回一单字符字符串的形式返回给定位置的那个字符。
var strValue="hello";
console.log(strValue.charAt(1);//"e";
2.charCodeAt()返回字符编码(ASCII)
var strValue="hello";
console.log(strValue.charCodeAt(1);//"101";
var str1="a";
console.log(str1.charCodeAt());//"97"
字符串操作方法
1.concat(),将一或多个字符串拼接起来(+号更简便易行)
var str1='hello';
var str2=' bule';
var str3=str1+str2;
var str4=str1.concat(str2);
console.log(str3);//hello bule
console.log(str4);//hello bule
2.slice(),substr(),substring()返回一个子字符串,不影响原字符串。
接受两个参数,第一个参数为指定字符串开始的位置。第二个参数:slice和substring为指定的是字符串最后一个字符后面的位置,substr为指定的是返回的字符个数。
var str1='hello bule';
console.log(str1.slice(3));//'lo bule';
console.log(str1.substring(3));//'lo bule'
console.log(str1.substr(3));//'lo bule';
console.log(str1.slice(3,5));//'lo';
console.log(str1.substring(3,5));//'lo'
console.log(str1.substr(3,5));//'lo bu';
字符串位置
1.indexOf()从字符串的开头向后搜索,lastIndexOf()从字符串的末尾向前搜索
var str1='hello bule';
console.log(str1.indexOf('l'));//2
console.log(str1.lastIndexOf('l'));//8
trim()方法
这个方法会创建一个字符串的副本,删除前置及后缀的所有结果,然后返回结果。
var str1=' hello bule ';
console.log(str1.trim());//"hello bule"