字符串常用方法
length 属性返回字符串的长度
var str = "qwertyuiop"
var str2 = "1234567890"
console.log(str.length);//10
console.log(str2.length);//10
indexOf() 方法返回字符串中指定文本首次出现的索引(位置):
var str = "Welcome to China.";
var pos = str.indexOf("Welcome");
console.log(pos);//0
lastIndexOf() 方法返回指定文本在字符串中最后一次出现的索引:
var str = "hello world.";
var pos2 = str.lastIndexOf("o");
console.log(pos2);//7
如果未找到文本, indexOf() 和 lastIndexOf() 均返回 -1。
indexOf() 和 lastIndexOf()都接受作为检索起始位置的第二个参数。
lastIndexOf() 方法向后进行检索(从尾到头),这意味着:假如第二个参数是 5,则从位置 5开始检索,直到字符串的起点。
var str = "The full name of China is the People's Republic of China.";
var pos = str.lastIndexOf("China", 50);
console.log(pos);//17
valueOf()返回某个字符串对象的原始值。
valueOf() 方法通常由 JavaScript 在后台自动进行调用,而不是显式地处于代码中。
语法:stringObject.valueOf()
search() 方法搜索特定值的字符串,并返回匹配的位置:
var str = "Celebrate National Day";
var pos = str.search("National");//10
// var pos = str.search("hello");//-1
console.log(pos);
您发现了什么没有?
两种方法,indexOf() 与 search(),表面效果是相等的。
这两种方法是不相等的。区别在于:
search() 方法无法设置第二个开始位置参数。
indexOf() 方法无法设置更强大的搜索值(正则表达式)。
提取部分字符串有三种方法:
slice(start, end)
substring(start, end)
substr(start, length)
slice( 开始位置 ,结束位置 )
提取字符串的某个部分并在新字符串中返回被提取的部分。
var str = "Celebrate National Day";
var res = str.slice(10,19);
console.log(res);
如果省略第二个参数,则该方法将裁剪字符串的剩余部分
var str = "Celebrate National Day";
var res = str.slice(10);
console.log(res);//National Day
提示:负值位置不适用 Internet Explorer 8 及其更早版本。
substring() 方法
substring() 类似于 slice()。
不同之处在于 substring() 无法接受负的索引。
如果省略第二个参数,则该 substring() 将裁剪字符串的剩余部分。
substr() 方法
substr() 类似于 slice()。
不同之处在于第二个参数规定被提取部分的长度。
var str = "0123456789";
var res = str.substr(7,2);
console.log(res);//78
replace() 方法不会改变调用它的字符串。它返回的是新字符串。
str = "Celebrate National Day";
var n = str.replace("Day", "Day ye");
console.log(n);//Celebrate National Day ye
如需执行大小写不敏感的替换,请使用正则表达式 /i(大小写不敏感):
str = "Celebrate National Day";
var n = str.replace(/day/i, "Day ye");
console.log(n);//Celebrate National Day ye
sup() 方法用于把字符串显示为上标。
var str="Hello world!"
document.write(str.sup())
sub()把字符串显示为下标。
var str="Hello world!"
document.write(str.sub())
通过 toUpperCase() 把字符串转换为大写:
var n1 = "Hello World!";
var n2 = n1.toUpperCase();
console.log(n2);//HELLO WORLD!
concat() 连接两个或多个字符串:
concat() 方法可用于代替加运算符。下面两行是等效的:
字符串是不可变的:字符串不能更改,只能替换。
var text1 = "Hello";
var text2 = "World";
text3 = text1.concat(" ",text2);
trim() 方法删除字符串两端的空白符:
var str = " Hello World! ";
alert(str.trim());
charAt() 方法返回字符串中指定下标(位置)的字符串:
var str = "HELLO WORLD";
str.charAt(0); // 返回 H
charCodeAt() 方法返回字符串中指定索引的字符 unicode 编码:
如果找不到字符, charAt() 返回空字符串。
var str = "HELLO JS";
str.charCodeAt(0);
console.log(str.charCodeAt( 6));//74
可以通过 split() 将字符串转换为数组:
var txt = "a,b,c,d,e"; // 字符串
txt.split(","); // 用逗号分隔
txt.split(" "); // 用空格分隔
txt.split("|"); // 用竖线分隔
console.log(txt.split(","));
如果省略分隔符,被返回的数组将包含 index [0] 中的整个字符串。
如果分隔符是 “”,被返回的数组将是间隔单个字符的数组:
var txt = "Hello"; // 字符串
txt.split(""); // 分隔为字符
console.log(txt.split(""));// ["H", "e", "l", "l", "o"]
如果字符串包含指定值,includes() 方法返回 true。
let text = "Hello world, welcome to the universe.";
text.includes("world") // 返回 true
text.includes("world", 12) // 返回 false
string.startsWith(需要搜索的值, 开始搜索的位置)
如果字符串以指定值开头,则 startsWith() 方法返回 true,否则返回 false:
注意:startsWith() 方法区分大小写。
let text = "Hello world, welcome to the universe.";
text.startsWith("Hello") // 返回 true
String.endsWith()
如果字符串以指定值结尾,则 endsWith() 方法返回 true,否则返回 false:
let text = "Hello world, welcome to the universe";
text.endsWith("universe")
console.log(text.endsWith("universe"));//返回 true
strike()使用删除线来显示字符串。
var txt="Hello World!"
document.write("<p>Strike: " + txt.strike() + "</p>")
link() 方法用于把字符串显示为超链接。
var str="Free Web Tutorials!"
document.write(str.link("http://www.w3school.com.cn"))
喜欢的小伙伴记得关注一下哦