JavaScript 字符串
JavaScript 字符串用于存储和操作文本
实例:
var arr="hello"
//您能够使用单引号或双引号:
var carname = "Porsche 911";
var carname = 'Porsche 911';
//您可以在字符串中使用引号,只要不匹配围绕字符串的引号即可:
var answer = "It's good to see you again!";
var answer = "He is called 'Bill'";
var answer = 'He is called "Bill"';
JavaScript 字符串(String)对象
字符串长度 (length)
内建属性 length 可返回字符串的长度:
实例:
var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var sln = txt.length;
length 属性返回字符串的长度:26
查找字符串中的字符串
indexOf() 方法返回字符串中指定文本首次出现的索引(位置):
实例
var str = "The full name of China is the People's Republic of China.";
var pos = str.indexOf("China");
indexOf() 方法返回指定文本首次出现的位置:17
JavaScript 从零计算位置。
0 是字符串中的第一个位置,1 是第二个,2 是第三个 …
lastIndexOf() 方法返回指定文本在字符串中最后一次出现的索引:
var str = "The full name of China is the People's Republic of China.";
var pos = str.lastIndexOf("China");
lastIndexOf() 返回指定文本最后出现的位置:51
如果未找到文本, indexOf() 和 lastIndexOf() 均返回 -1。
var str = "The full name of China is the People's Republic of China.";
var pos = str.indexOf("USA");
如果未找到文本,则 indexOf() 和 lastIndexOf() 都返回 -1:-1
** indexOf() 和 lastIndexOf() 两种方法都接受作为检索起始位置的第二个参数。**
var str = "The full name of China is the People's Republic of China.";
var pos = str.indexOf("China", 18);
indexOf() 方法接受第二个参数作为搜索的起始位置:51
lastIndexOf() 方法向后进行检索(从尾到头),这意味着:假如第二个参数是 50,则从位置 50 开始检索,直到字符串的起点。
var str = "The full name of China is the People's Republic of China.";
var pos = str.lastIndexOf("China", 50);
lastIndexOf() 方法接受第二个参数作为搜索的起始位置:17
请记住,lastIndexOf()方法向后搜索,因此位置 50 表示在第 50 位开始搜索,并搜索到开头。
位置 50 指的是从开头算起的位置 50。
检索字符串中的字符串
search() 方法搜索特定值的字符串,并返回匹配的位置:
var str = "The full name of China is the People's Republic of China.";
var pos = str.search("locate");
search() 方法返回字符串中指定文本第一次出现的位置:17
两种方法,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);
res 的结果是:Banana
substring() 方法
substring() 类似于 slice()。
不同之处在于 substring() 无法接受负的索引。
var str = "Apple, Banana, Mango";
var res = str.substring(7,13);
res 的结果是:Banana
substr() 方法
substr() 类似于 slice()。
不同之处在于第二个参数规定被提取部分的长度。
var str = "Apple, Banana, Mango";
var res = str.substr(7,6);
res 的结果是:Banana
替换字符串内容
replace() 方法用另一个值替换在字符串中指定的值:
str = "Please visit Microsoft!";
var n = str.replace("Microsoft", "W3School");
replace() 方法不会改变调用它的字符串。它返回的是新字符串。
转换为大写和小写
通过 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!");
所有字符串方法都会返回新字符串。它们不会修改原始字符串。
正式地说:字符串是不可变的:字符串不能更改,只能替换。
提取字符串字符
这是两个提取字符串字符的安全方法:
- charAt(position)
- charCodeAt(position)
charAt() 方法
charAt() 方法返回字符串中指定下标(位置)的字符串:
var str = "HELLO WORLD";
str.charAt(0); // 返回 H
把字符串转换为数组
可以通过 split() 将字符串转换为数组:
var txt = "a,b,c,d,e"; // 字符串
txt.split(","); // 用逗号分隔
txt.split(" "); // 用空格分隔
txt.split("|"); // 用竖线分隔
如果省略分隔符,被返回的数组将包含 index [0] 中的整个字符串。
如果分隔符是 “”,被返回的数组将是间隔单个字符的数组:
var txt = "Hello"; // 字符串
txt.split(""); // 分隔为字符