1.charAt()
接收一个参数,是基于0的字符位置,以单字符字符串的形式返回给定位置的那个字符
var stringValue = "click on me";
alert(stringValue.charAt(1)); //"l"
2.charCodeAt()
接受一个参数,是基于0的字符位置,返回的是对应字符的字符编码
var stringValue = "click on me";
alert(stringValue.charCodeAt(1)); //"108",l对应的字符编码为108
3.concat()
将一个或者多个字符串拼接起来,返回拼接得到的新的字符串。(字符拼接的话一般使用+操作符,比较简便)
var stringValue = "click on ";
var result = "me"
alert(result); //"click on me"
alert(stringValue); //"click on"
4.slice(),substring()
返回被操作字符串的一个子字符串,接收一个或者两个参数,第一个参数指定开始的位置,第二个参数(若指定的话)表示子字符串到哪里结束,即指定的是子字符串最后一个字符后面的位置。
若传入的参数是负数,那么slice()方法会将传入的负数与字符串的长度相加,substring()方法会把所有的负值转换成0.
var stringValue = "hello world";
alert(stringValue.slice(2)); //"llo world"
alert(stringValue.substring(2)); //"llo world"
alert(stringValue.slice(2,5)); //"llo"
alert(stringValue.substring(2,5)); //"llo"
alert(stringValue.slice(-3)); //"rld"
alert(stringValue.substring(-3)); //"hello world"
alert(stringValue.slice(3,-4)); //"lo w"
alert(stringValue.substring(3,-4)); //"hel",转换成substring(3,0)相当于substring(0,3)
5.substr()
返回被操作字符串的一个子字符串,接收一个或者两个参数,第一个参数指定开始的位置,第二个参数指定的是返回的字符个数。
var stringValue = "hello world";
alert(stringValue.substr(2)); //"llo world"
alert(stringValue.substr(2,6)); //"llo wo"
alert(stringValue.substr(-2)); //"ld"
alert(stringValue.substr(2,-6)); //""(空字符串)
6. indexOf(),lastIndexOf()
从一个字符串中搜索给定的字符串,然后返回子字符串的位置(如果没有找到这个子字符串,那么会返回-1)。indexOf()方法从字符串的开头向后搜索子字符串,而lastIndexOf()方法则是从后向前搜索子字符串。
这两个方法都可以接收可以选择的第二个参数,表示从字符串的哪个位置开始搜索。
var stringValue = "hello world";
alert(stringValue.indexOf("0")); //4
alert(stringValue.lastIndexOf("0")); //7
alert(stringValue.indexOf("0",6)); //7
alert(stringValue.lastIndexOf("0",6)); //4
7.trim()
创建一个字符串的副本,删除前置及后缀的所有空格,然后返回结果。
var stingValue = " hello world ";
var anotherStingValue= stingValue.trim();
alert(stingValue); //" hello world "
alert(anotherStingValue); //"hello world"
8. toLowerCase() toLocalLowerCase() toUpperCase() toLocalUpperCase()
toLowerCase()和toLocalLowerCase()对输入的字符串进行小写操作;
toUpperCase()和toLocalUpperCase()对输入的字符串进行大写操作;
var stringValue = "hello world";
alert(stringValue.toLocalUpperCase()); //"HELLO WORLD"
alert(stringValue.toUpperCase()); //"HELLO WORLD"
alert(stringValue.toLocalLowerCase()); //"hello world"
alert(stringValue.toLowerCase()); //"hello world"
toLocalLowerCase()和toLocalUpperCase()方法是针对特定地区的实现,对某些地区来说,进行大小写转换需要一定的规则,所以在不知道自己的代码将在什么语言环境下使用的情况下还是使用这两个方法比较好。