JS学习——字符串方法

学习内容来源:JavaScript 字符串方法JavaScript 字符串搜索

JavaScript 字符串方法

字符串长度

length 属性返回字符串的长度

// sln = 26
var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var sln = txt.length;

查找字符串中的字符串

indexOf() 方法返回字符串中指定文本首次出现的索引(位置)
lastIndexOf() 方法返回指定文本在字符串中最后一次出现的索引
两种方法都接受作为检索起始位置的第二个参数
如果未找到文本, indexOf() 和 lastIndexOf() 均返回 -1

// pos = 17
var str = "The full name of China is the People's Republic of China.";
var pos = str.indexOf("China");

// pos = 51
var str = "The full name of China is the People's Republic of China.";
var pos = str.indexOf("China");

// pos = -1
var str = "The full name of China is the People's Republic of China.";
var pos = str.indexOf("USA");

// 接受作为检索起始位置的参数,pos = 51
var str = "The full name of China is the People's Republic of China.";
var pos = str.indexOf("China", 18);

// lastIndexOf() 方法从尾到头进行检索,直到字符串的起点,pos = 17
var str = "The full name of China is the People's Republic of China.";
var pos = str.lastIndexOf("China", 50);

String.match()

match() 方法根据正则表达式在字符串中搜索匹配项,并将匹配项作为 Array 对象返回。

// match() 方法只返回字符串中的第一个匹配项
let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/)    // 返回数组 [ain]

// 对 "ain" 执行全局搜索
let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/g)    // 返回数组 [ain,ain,ain]

// 对 "ain" 执行不区分大小写的全局搜索
let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/gi)   // 返回数组 [ain,AIN,ain,ain]

String.includes()

如果字符串包含指定值,includes() 方法返回 true。

该函数接受两个参数:

  • 需要搜索的字符串(必需)
  • 开始搜索的位置(可选。默认为 0)
let text = "Hello world, welcome to the universe.";
text.includes("world")    // 返回 true

// 检查字符串是否包含 "world",从位置 12 开始搜索
let text = "Hello world, welcome to the universe.";
text.includes("world", 12)    // 返回 false

String.startsWith()

如果字符串以指定值开头,则 startsWith() 方法返回 true,否则返回 false

注意:startsWith() 方法区分大小写。

该函数接受两个参数:

  • 需要搜索的字符串(必需)
  • 开始搜索的位置(可选。默认为 0)
let text = "Hello world, welcome to the universe.";
text.startsWith("Hello")   // 返回 true

// 从位置 5 开始搜索
let text = "Hello world, welcome to the universe.";
text.startsWith("world", 5)    // 返回 false

String.endsWith()

如果字符串以指定值结尾,则 endsWith() 方法返回 true,否则返回 false

注意:endsWith() 方法区分大小写。

该函数接受两个参数:

  • 需要搜索的值(必需)
  • 要搜索的长度(可选)
var text = "John Doe";
text.endsWith("Doe")    // 返回 true

// 搜索字符串前 11 位
let text = "Hello world, welcome to the universe.";
text.endsWith("world", 11)    // 返回 true

检索字符串中的字符串

search() 方法搜索特定值的字符串,并返回匹配的位置
如果未找到文本, 返回 -1

// pos = -1
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() 提取字符串的某个部分并在新字符串中返回被提取的部分。
该方法设置两个参数:起始索引(开始位置),终止索引(结束位置)。返回字符串包括开始位置,不包括结束位置。

// res = "Banana"
var str = "Apple, Banana, Mango";
var res = str.slice(7,13);

// 如果某个参数为负,则从字符串的结尾开始计数。res = "Banana"
var str = "Apple, Banana, Mango";
var res = str.slice(-13,-7);

// 如果省略第二个参数,则该方法将裁剪字符串的剩余部分。res = "Banana, Mango"
var str = "Apple, Banana, Mango";
var res = str.slice(7);

// 或者从结尾计数。res = "Banana, Mango"
var str = "Apple, Banana, Mango";
var res = str.slice(-13);

substring() 方法

substring() 类似于 slice()。
不同之处在于 substring() 无法接受负的索引。

// res = "Banana"
var str = "Apple, Banana, Mango";
var res = str.substring(7,13);

// 如果省略第二个参数,则该 substring() 将裁剪字符串的剩余部分。res = "Banana, Mango"
var str = "Apple, Banana, Mango";
var res = str.substring(7);

substr() 方法

substr() 类似于 slice()。
不同之处在于第二个参数规定被提取部分的长度。

注意:第二个参数不能为负,因为它定义的是长度。

// res = "Banana"
var str = "Apple, Banana, Mango";
var res = str.substr(7,6);

// 如果省略第二个参数,则该 substr() 将裁剪字符串的剩余部分。res = "Banana, Mango"
var str = "Apple, Banana, Mango";
var res = str.substr(7);

// 如果首个参数为负,则从字符串的结尾计算位置。res= "Mango"
var str = "Apple, Banana, Mango";
var res = str.substr(-5);

替换字符串内容

replace() 方法用另一个值替换在字符串中指定的值。
replace() 方法不会改变调用它的字符串。它返回的是新字符串。
默认地,replace() 只替换首个匹配。
默认地,replace() 对大小写敏感。

// n = "Please visit W3School!"
str = "Please visit Microsoft!";
var n = str.replace("Microsoft", "W3School");

// 默认地,replace() 只替换首个匹配。n = "Please visit W3School and Microsoft!"
str = "Please visit Microsoft and Microsoft!";
var n = str.replace("Microsoft", "W3School");

// 默认地,replace() 对大小写敏感。n = "Please visit Microsoft!"
str = "Please visit Microsoft!";
var n = str.replace("MICROSOFT", "W3School");

// 如需执行大小写不敏感的替换,请使用正则表达式 /i(大小写不敏感), n = "Please visit W3School!"
str = "Please visit Microsoft!";
var n = str.replace(/MICROSOFT/i, "W3School");

// 如需替换所有匹配,请使用正则表达式的 g 标志(用于全局搜索), n = "Please visit W3School and W3School!";
str = "Please visit Microsoft and Microsoft!";
var n = str.replace(/Microsoft/g, "W3School");

转换为大写和小写

1、通过 toUpperCase() 把字符串转换为大写

// text2 = "HELLO WORLD!"
var text1 = "Hello World!";       // 字符串
var text2 = text1.toUpperCase();  // text2 是被转换为大写的 text1

2、通过 toLowerCase() 把字符串转换为小写

// text2 = "hello world!"
var text1 = "Hello World!";       // 字符串
var text2 = text1.toLowerCase();  // text2 是被转换为小写的 text1

concat() 方法

concat() 连接两个或多个字符串。
concat() 方法可用于代替加运算符。

// text3 = "Hello World"
var text1 = "Hello";
var text2 = "World";
text3 = text1.concat(" ",text2);

// 下面两行是等效的
var text = "Hello" + " " + "World!";
var text = "Hello".concat(" ","World!");

String.trim()

trim() 方法删除字符串两端的空白符

// n = "Hello World!"
var str = "       Hello World!        ";
var n = 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

属性访问(Property Access)

使用属性访问有点不太靠谱:

  • 不适用 Internet Explorer 7 或更早的版本
  • 它让字符串看起来像是数组(其实并不是)
  • 如果找不到字符,[ ] 返回 undefined,而 charAt() 返回空字符串。
  • 它是只读的。str[0] = "A" 不会产生错误(但也不会工作!)
var str = "HELLO WORLD";
str[0] = "A";             // 不产生错误,但不会工作
str[0];                   // 返回 H
str[20];                  // 返回 undefined
str.charAt(20)            // 返回 ""

把字符串转换为数组

可以通过 split() 将字符串转换为数组。
如果省略分隔符,被返回的数组将包含 index [0] 中的整个字符串。
如果分隔符是 "",被返回的数组将是间隔单个字符的数组。

var txt = "a,b,c,d,e";   // 字符串
txt.split(",");          // 以逗号分隔
txt.split(" ");          // 以空格分隔
txt.split("|");          // 以竖线分隔
txt.split("");           // 分隔为字符
posted @ 2022-03-03 15:13  一只离离离  阅读(59)  评论(0编辑  收藏  举报