var str = newString("RUNOOB"); console.log("str.charAt(0) 为:" + str.charAt(0)); //R
charCodeAt()返回在指定的位置的字符的 Unicode 编码
1 2
var str = newString("RUNOOB"); console.log("str.charCodeAt(0) 为:" + str.charCodeAt(0)); // 82
concat()连接两个或更多字符串,并返回新的字符串
1 2 3 4
var str1 = newString( "RUNOOB" ); var str2 = newString( "GOOGLE" ); var str3 = str1.concat( str2 ); console.log("str1 + str2 : "+str3) // RUNOOBGOOGLE
indexOf() 返回某个指定的字符串值在字符串中首次出现的位置
1 2 3 4
var str1 = newString( "RUNOOB" );
var index = str1.indexOf( "OO" ); console.log("查找的字符串位置 :" + index ); // 3
lastIndexOf()从后向前搜索字符串,并从起始位置(0)开始计算返回字符串最后出现的位置
1 2 3 4 5 6
var str1 = newString( "This is string one and again string" ); var index = str1.lastIndexOf( "string" ); console.log("lastIndexOf 查找到的最后字符串位置 :" + index ); // 29 index = str1.lastIndexOf( "one" ); console.log("lastIndexOf 查找到的最后字符串位置 :" + index ); // 15
localeCompare()用本地特定的顺序来比较两个字符串
1 2 3 4 5
var str1 = newString( "This is beautiful string" ); var index = str1.localeCompare( "This is beautiful string");
console.log("localeCompare first :" + index ); // 0
match()查找找到一个或多个正则表达式的匹配
1 2
var str="The rain in SPAIN stays mainly in the plain"; var n=str.match(/ain/g); // ain,ain,ain
replace()替换与正则表达式匹配的子串
1 2 3 4
var re = /(\w+)\s(\w+)/; var str = "zara ali"; var newstr = str.replace(re, "$2, $1"); console.log(newstr); // ali, zara
search()检索与正则表达式相匹配的值
1 2 3 4 5 6 7
var re = /apples/gi; var str = "Apples are round, and apples are juicy."; if (str.search(re) == -1 ) { console.log("Does not contain Apples" ); } else { console.log("Contains Apples" ); }
slice()提取字符串的片断,并在新的字符串中返回被提取的部分
1
string.slice( beginslice [, endSlice] );
参数详情
beginSlice - 开始提取的从零开始的索引。
endSlice - 结束提取的从零开始的索引。如果省略,则将切片提取到字符串的末尾。
1 2 3 4 5 6
var str = "Apples are round, and apples are juicy."; var sliced = str.slice(3, -2); console.log(sliced);
// 输出如下: les are round, and apples are juic
split()把字符串分割为子字符串数组
1 2 3
var str = "Apples are round, and apples are juicy."; var splitted = str.split(" ", 3); console.log(splitted) // [ 'Apples', 'are', 'round,' ]
substr()从起始索引号提取字符串中指定数目的字符
1
string.substr(start[, length]);
参数详情
start - 开始提取字符的位置(0到1之间的整数,小于字符串的长度)。
length - 要提取的字符数。
注意 :如果start为负数,则substr将其用作字符串末尾的字符索引。
1 2 3 4 5 6 7
var str = "Apples are round, and apples are juicy."; console.log("(1,2): " + str.substr(1,2)); console.log("(-2,2): " + str.substr(-2,2));
// 输出 (1,2): pp (-2,2): y.
substring()提取字符串中两个指定的索引号之间的字符
1 2 3 4
var str = "RUNOOB GOOGLE TAOBAO FACEBOOK"; console.log("(1,2): " + str.substring(1,2)); // U console.log("(0,10): " + str.substring(0, 10)); // RUNOOB GOO console.log("(5): " + str.substring(5)); // B GOOGLE TAOBAO FACEBOOK
toLocaleLowerCase()
根据主机的语言环境把字符串转换为小写,只有几种语言(如土耳其语)具有地方特有的大小写映射。
1 2
var str = "Runoob Google"; console.log(str.toLocaleLowerCase( )); // runoob google
toLocaleUpperCase()
据主机的语言环境把字符串转换为大写,只有几种语言(如土耳其语)具有地方特有的大小写映射。
1 2
var str = "Runoob Google"; console.log(str.toLocaleUpperCase( )); // RUNOOB GOOGLE
toLowerCase()把字符串转换为小写
1 2
var str = "Runoob Google"; console.log(str.toLowerCase( )); // runoob google
toString()返回字符串
1 2
var str = "Runoob"; console.log(str.toString( )); // Runoob
toUpperCase()把字符串转换为大写
1 2
var str = "Runoob Google"; console.log(str.toUpperCase( )); // RUNOOB GOOGLE
valueOf()返回指定字符串对象的原始值
1 2
var str = newString("Runoob"); console.log(str.valueOf( )); // Runoob