字符串方法和属性
字符串长度
length
属性返回字符串的长度:
var str = 'jefighnibrpbk';
var res = str.length;
查找字符串中的字符串
indexOf()
方法返回字符串中指定文本首次出现的索引(位置):
var str = "China 100th Anniversary of the Founding of the People's Republic of China";
var res = str.indexOf('China');
console.log(res); //0
lastIndexOf()
方法返回指定文本在字符串中最后一次出现的索引(位置)
var str = "China 100th Anniversary of the Founding of the People's Republic of China";
var res = str.lastIndexOf("China");
console.log(res); //68
如果未找到文本,
indexOf()
和
lastIndexOf()
均返回
-1
。
var str = "China 100th Anniversary of the Founding of the People's Republic of China";
var res = str.indexOf('USA');
console.log(res); //-1
两种方法都
接受
作为检索起始位置的
第二个参数
。
indexOf()
方法向后进行检索(
从头到尾
),这意味着:假如第二个参数是 10,则从位置 10 开始检索,直到字符串的终点
lastIndexOf()
方法向后进行检索(
从尾到头
),这意味着:假如第二个参数是 50,则从位置 50 开始检索,直到字符串的起点
var res = str.indexOf("China", 10);
var res = str.lastIndexOf("China", 50);
检索字符串中的字符串
search()
方法搜索特定值的字符串,并返回匹配的位置
var str = "The full name of China is the People's Republic of China.";
var res = str.search("name");
两种方法,
indexOf()
与
search()
,是相等的。
区别在于:
- search() 方法无法设置第二个开始位置参数
- indexOf() 方法无法设置更强大的搜索值(正则表达式)
提取部分字符串
有三种提取部分字符串的方法:
- slice(start, end)
- substring(start, end)
- substr(start, length
slice()
提取字符串的某个部分并在新字符串中返回被提取的部分。
// 位置 7 到位置 13 的片段
var res = str.slice(7,13); // Banana
// 如果某个参数为负,则从字符串的结尾开始计数。
var res = str.slice(-13,-7); // Banana
// 如果省略第二个参数,则该方法将裁剪字符串的剩余部分:
var res = str.slice(7);
// 从结尾计数
var res = str.slice(-13);
substring()
类似于
slice()
// 不同之处在于 substring() 无法接受负的索引。
var res = str.substring(7,13);
// 如果省略第二个参数,则该 substring() 将裁剪字符串的剩余部分。
var res = str.substring(7);
substr()
类似于
slice()
// 不同之处在于第二个参数规定被提取部分的长度。
var res = str.substr(7,6);//Banana
// 如果省略第二个参数,则该 substr() 将裁剪字符串的剩余部分。
var res = str.substr(7); //Banana, Mango
// 如果首个参数为负,则从字符串的结尾计算位置。
var res = str.substr(-5); // Mango
第二个参数不能为负,因为它定义的是长度。
替换字符串内容
replace()
方法用另一个值替换在字符串中指定的值。
str = "Please visit Microsoft!";
var n = str.replace("Microsoft", "W3School");
replace()
方法不会改变调用它的字符串。它返回的是新字符串。
// 1.默认地,replace() 只替换首个匹配:
str = "Please visit Microsoft and Microsoft!";
var n = str.replace("Microsoft", "W3School");
// Please visit W3School and Microsoft!
// 2.默认地,replace() 对大小写敏感。因此不对匹配 MICROSOFT:
str = "Please visit Microsoft!";
var n = str.replace("MICROSOFT", "W3School");
// 不会变
// 3.如需执行大小写不敏感的替换,请使用正则表达式 /i(大小写不敏感):
str = "Please visit Microsoft!";
var n = str.replace(/MICROSOFT/i, "W3School");
// 4.如需替换所有匹配,请使用正则表达式的 g 标志(用于全局搜索):
str = "Please visit Microsoft and Microsoft!";
var n = str.replace(/Microsoft/g, "W3School");
转换为大写和小写
通过
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());
Internet Explorer 8 或更低版本不支持 trim() 方法。
提取字符串字符
这是两个提取字符串字符的安全方法:
charAt(position)
charCodeAt(position)
charAt()
方法返回字符串中指定下标(位置)的字符串:
var str = "HELLO WORLD";
str.charAt(0); // 返回 H
charCodeAt()
方法返回字符串中指定索引的字符 unicode 编码:
var str = "HELLO WORLD";
str.charCodeAt(0); // 返回 72
把字符串转换为数组
可以通过
split()
将字符串转换为数组:
var txt = "a,b,c,d,e"; // 字符串
txt.split(","); // 用逗号分隔
txt.split(" "); // 用空格分隔
txt.split("|"); // 用竖线分隔
// ["a", "b", "c", "d", "e"]
// ["a,b,c,d,e"]
// ["a,b,c,d,e"]
var txt = "Hello"; // 字符串
txt.split(""); // 分隔为字符
// ["H", "e", "l", "l", "o"]
自己总结学习,如有冲突,请联系删除。