字符串方法帮助您处理字符串。


字符串方法和属性

原始值,比如“Bill Gates”,无法拥有属性和方法(因为它们不是对象)。
但是通过 JavaScript,方法和属性也可用于原始值,因为在执行方法和属性时 JavaScript 将原始值视为对象。

字符串长度

length 属性返回字符串的长度:
var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var sln = txt.length;

查找字符串中的字符串

indexOf() 方法返回字符串中指定文本首次出现的索引(位置):
var str = "The full name of China is the People's Republic of China.";
var pos = str.indexOf("China");
JavaScript 从零计算位置。
0 是字符串中的第一个位置,1 是第二个,2 是第三个 ...
lastIndexOf() 方法返回指定文本在字符串中最后一次出现的索引:
var str = "The full name of China is the People's Republic of China.";
var pos = str.lastIndexOf("China");
如果未找到文本, indexOf()lastIndexOf() 均返回 -1。
var str = "The full name of China is the People's Republic of China.";
var pos = str.indexOf("USA");
两种方法都接受作为检索起始位置的第二个参数。
var str = "The full name of China is the People's Republic of China.";
var pos = str.indexOf("China", 18);
lastIndexOf() 方法向后进行检索(从尾到头),这意味着:假如第二个参数是 50,则从位置 50 开始检索,直到字符串的起点。
var str = "The full name of China is the People's Republic of China.";
var pos = str.lastIndexOf("China", 50);

检索字符串中的字符串

search() 方法搜索特定值的字符串,并返回匹配的位置:
var str = "The full name of China is the People's Republic of China.";
var pos = str.search("locate");
您注意到了吗?
两种方法,indexOf()search(),是相等的
这两种方法是不相等的。区别在于:
  • search() 方法无法设置第二个开始位置参数。
  • indexOf() 方法无法设置更强大的搜索值(正则表达式)。
您将在正则表达式的章节学习到这些更强大的检索值。

提取部分字符串

有三种提取部分字符串的方法:
  • slice(start, end)
  • substring(start, end)
  • substr(start, length)

slice() 方法

slice() 提取字符串的某个部分并在新字符串中返回被提取的部分。
该方法设置两个参数:起始索引(开始位置),终止索引(结束位置)。
这个例子裁剪字符串中位置 7 到位置 13 的片段:
var str = "Apple, Banana, Mango";
var res = str.slice(7,13);
res 的结果是:
Banana
如果某个参数为负,则从字符串的结尾开始计数。
这个例子裁剪字符串中位置 -12 到位置 -6 的片段:
var str = "Apple, Banana, Mango";
var res = str.slice(-13,-7);
如果省略第二个参数,则该方法将裁剪字符串的剩余部分:
var res = str.slice(7);
或者从结尾计数:
var res = str.slice(-13);

substring() 方法

substring() 类似于 slice()
不同之处在于 substring() 无法接受负的索引。
var str = "Apple, Banana, Mango";
var res = str.substring(7,13);
res 的结果是:
Banana
如果省略第二个参数,则该 substring() 将裁剪字符串的剩余部分。

substr() 方法

substr() 类似于 slice()
不同之处在于第二个参数规定被提取部分的长度
var str = "Apple, Banana, Mango";
var res = str.substr(7,6);
res 的结果是:
Banana
如果省略第二个参数,则该 substr() 将裁剪字符串的剩余部分。
var str = "Apple, Banana, Mango";
var res = str.substr(7);
res 的结果是:
Banana, Mango
如果首个参数为负,则从字符串的结尾计算位置。
var str = "Apple, Banana, Mango";
var res = str.substr(-5);
res 的结果是:
Mango
第二个参数不能为负,因为它定义的是长度。

替换字符串内容

replace() 方法用另一个值替换在字符串中指定的值:
str = "Please visit Microsoft!";
var n = str.replace("Microsoft", "W3School");
replace() 方法不会改变调用它的字符串。它返回的是新字符串。
默认地,replace() 只替换首个匹配
str = "Please visit Microsoft and Microsoft!";
var n = str.replace("Microsoft", "W3School");
默认地,replace() 对大小写敏感。因此不对匹配 MICROSOFT:
str = "Please visit Microsoft!";
var n = str.replace("MICROSOFT", "W3School");
如需执行大小写不敏感的替换,请使用正则表达式 /i(大小写不敏感):
str = "Please visit Microsoft!";
var n = str.replace(/MICROSOFT/i, "W3School");
请注意正则表达式不带引号。
如需替换所有匹配,请使用正则表达式的 g 标志(用于全局搜索):
str = "Please visit Microsoft and Microsoft!";
var n = str.replace(/Microsoft/g, "W3School");
您将在 JavaScript 正则表达式这一章学到更多有关正则表达式的内容。

转换为大写和小写

通过 toUpperCase() 把字符串转换为大写:
var text1 = "Hello World!";       // 字符串
var text2 = text1.toUpperCase();  // text2 是被转换为大写的 text1
通过 toLowerCase() 把字符串转换为小写:
var text1 = "Hello World!";       // 字符串
var text2 = text1.toLowerCase();  // text2 是被转换为小写的 text1

concat() 方法
concat() 连接两个或多个字符串:
var text1 = "Hello";
var text2 = "World";
text3 = text1.concat(" ",text2);
concat() 方法可用于代替加运算符。下面两行是等效的:
var text = "Hello" + " " + "World!";
var text = "Hello".concat(" ","World!");
所有字符串方法都会返回新字符串。它们不会修改原始字符串。
正式地说:字符串是不可变的:字符串不能更改,只能替换。

String.trim()

trim() 方法删除字符串两端的空白符:
var str = "       Hello World!        ";
alert(str.trim());

提取字符串字符

这是两个提取字符串字符的安全方法:
  • charAt(position)
  • charCodeAt(position)

charAt() 方法

charAt() 方法返回字符串中指定下标(位置)的字符串:
var str = "HELLO WORLD";
str.charAt(0);            // 返回 H

charCodeAt() 方法

charCodeAt() 方法返回字符串中指定索引的字符 unicode 编码:
var str = "HELLO WORLD";

str.charCodeAt(0);         // 返回 72

把字符串转换为数组

可以通过 split() 将字符串转换为数组:
var txt = "a,b,c,d,e";   // 字符串
txt.split(",");          // 用逗号分隔
txt.split(" ");          // 用空格分隔
txt.split("|");          // 用竖线分隔
如果省略分隔符,被返回的数组将包含 index [0] 中的整个字符串。
如果分隔符是 "",被返回的数组将是间隔单个字符的数组:
var txt = "Hello";       // 字符串
txt.split("");           // 分隔为字符