length:
返回字符串的长度
var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var sln = txt.length;
console.log(sln); //26
indexOf()
:返回字符串中指定文本首次出现的索引(位置)zhu
var str = "The full name of China is the People's Republic of China.";
var pos = str.indexOf("China"); //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"); //51
注:如果未找到文本,
indexOf()
和lastIndexOf()
均返回 -1。
-
indexOf()和lastIndexOf()
都接受作为检索起始位置的第二个参数
var str = "The full name of China is the People's Republic of China.";
var pos = str.indexOf("China", 18); //51
var str = "The full name of China is the People's Republic of China.";
var pos = str.lastIndexOf("China", 50); //17
-
search()
:搜索特定值的字符串,并返回匹配的位置
var str = "The full name of China is the People's Republic of China.";
var pos = str.search("locate"); //-1
indexOf()
与search()
,区别在于:
- search() :无法设置第二个开始位置参数。
- indexOf() :无法设置更强大的搜索值(正则表达式)。
slice()
提取字符串的某个部分并在新字符串中返回被提取的部分。
该方法设置两个参数:起始索引(开始位置),终止索引(结束位置)。
var str = "Apple, Banana, Mango";
var res = str.slice(7, 13); //Banana
如果某个参数为负,则从字符串的结尾开始计数。
var str = "Apple, Banana, Mango";
var res = str.slice(-13,-7); //Banana
如果省略第二个参数,则该方法将裁剪字符串的剩余部分
var str = "Apple, Banana, Mango";
var res = str.slice(7); //Banana, Mango
-
substring()
类似于slice(),不同的是substring()
无法接受负的索引
var str = "Apple, Banana, Mango";
var res = str.substring(7, 13); //Banana
-
substr()
类似于slice(),
不同之处在于第二个参数规定被提取部分的长度。
var str = "Apple, Banana, Mango";
var res = str.substr(7, 6); //Banana
如果首个参数为负,则从字符串的结尾计算位置
var str = "Apple, Banana, Mango";
var res = str.substr(-5); //Mango
-
replace():
用另一个值替换在字符串中指定的值,默认值替换首个匹配,且对大小写敏感
var str = "Please visit Microsoft!";
var n = str.replace("Microsoft", "W3School"); //Please visit W3School!
toUpperCase()
:把字符串转换为大写
var text1 = "Hello World!"; // 字符串
var text2 = text1.toUpperCase(); // text2 是被转换为大写的 text1
console.log(text2); //HELLO WORLD!
toLowerCase():
把字符串转换为小写
var text1 = "Hello World!"; // 字符串
var text2 = text1.toLowerCase(); // text2 是被转换为小写的 text1
console.log(text2); //hello world!
-
concat()
:连接两个或多个字符串
var text1 = "Hello";
var text2 = "World";
var text3 = text1.concat(" ", text2);
console.log(text3); //Hello World
charAt()
:方法返回字符串中指定下标(位置)的字符串
var str = "HELLO WORLD";
var newStr = str.charAt(0); // 返回 H
charCodeAt()
:方法返回字符串中指定索引的字符 unicode 编码
var str = "HELLO WORLD";
var newStr = str.charCodeAt(0); //72
-
split():
将字符串转换为数组
var txt = "a,b,c,d,e"; // 字符串
var txt1 = txt.split(","); // 用逗号分隔
includes():
如果字符串包含指定值,返回 true
let text = "Hello world, welcome to the universe.";
console.log(text.includes("world")) // 返回 true
let text = "Hello world, welcome to the universe.";
console.log(text.includes("world", 12)) // 返回 false
-
startsWith():
如果字符串以指定值开头,返回true
,否则返回false(区分大小写)
let text = "Hello world, welcome to the universe.";
console.log(text.startsWith("Hello")) // 返回 true
let text = "Hello world, welcome to the universe.";
console.log(text.startsWith("world", 6)) // 返回 false
endsWith()
如果字符串以指定值结尾,返回true
,否则返回false
var text = "Bill Gates";
text.endsWith("Gates") // 返回 true