JavaScript中23个String方法(上)

简单介绍

JavaScript 中的String类型用于表示文本型的数据。它是由无符号整数值(16bit)作为元素而组成的集合。字符串中的每个元素在字符串中占据一个位置. 第一个元素的 index 值是 0,下一个元素的 index 值是 1,以此类推。字符串的长度就是字符串中所含的元素个数.你可以通过 String 字面值或者 String 对象两种方式创建一个字符串。

方法介绍(上)

1、charAt()

字符串中的字符从左向右索引,第一个字符的索引值为 0,最后一个字符(假设该字符位于字符串 stringName 中)的索引值为 stringName.length - 1。 如果指定的 index 值超出了该范围,则返回一个空字符串

```JavaScript var anyString = "Brave new world";

console.log("The character at index 0 is '" + anyString.charAt(0) + "'"); console.log("The character at index 1 is '" + anyString.charAt(1) + "'"); console.log("The character at index 2 is '" + anyString.charAt(2) + "'"); console.log("The character at index 3 is '" + anyString.charAt(3) + "'"); console.log("The character at index 4 is '" + anyString.charAt(4) + "'"); console.log("The character at index 999 is '" + anyString.charAt(999) + "'"); ```

输出 JavaScript The character at index 0 is 'B' The character at index 1 is 'r' The character at index 2 is 'a' The character at index 3 is 'v' The character at index 4 is 'e' The character at index 999 is ''

2、charCodeAt()

返回 0 到 65535 之间的整数,表示给定索引处的 UTF-16 代码单元

```JavaScript "ABC".charCodeAt(0) // returns 65:"A"

"ABC".charCodeAt(1) // returns 66:"B"

"ABC".charCodeAt(2) // returns 67:"C"

"ABC".charCodeAt(3) // returns NaN ```

3、codePointAt()

如果在指定的位置没有元素则返回undefined。如果在索引处开始没有 UTF-16 代理对,将直接返回在那个索引处的编码单元。

JavaScript 'ABC'.codePointAt(1); // 66 '\uD800\uDC00'.codePointAt(0); // 65536 'XYZ'.codePointAt(42); // undefined

4、indexOf()

返回调用它的String对象中第一次出现的指定值的索引,从 fromIndex 处进行搜索。如果未找到该值,则返回 -1

```JavaScript const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';

const searchTerm = 'dog'; const indexOfFirst = paragraph.indexOf(searchTerm);

console.log(The index of the first "${searchTerm}" from the beginning is ${indexOfFirst}); // expected output: "The index of the first "dog" from the beginning is 40"

console.log(The index of the 2nd "${searchTerm}" is ${paragraph.indexOf(searchTerm, (indexOfFirst + 1))}); // expected output: "The index of the 2nd "dog" is 52" ```

5、lastIndexOf()

返回调用String对象的指定值最后一次出现的索引,在一个字符串中的指定位置 fromIndex处从后向前搜索。如果没找到这个特定值则返回-1

JavaScript 'canal'.lastIndexOf('a'); // returns 3(没有指明 fromIndex 则从末尾 l 处开始反向检索到的第一个 a 出现在 l 的后面,即 index 为 3 的位置) 'canal'.lastIndexOf('a', 2); // returns 1(指明 fromIndex 为 2 则从 n 处反向向回检索到其后面就是 a,即 index 为 1 的位置) 'canal'.lastIndexOf('a', 0); // returns -1(指明 fromIndex 为 0 则从 c 处向左回向检索 a 发现没有,故返回-1) 'canal'.lastIndexOf('x'); // returns -1 'canal'.lastIndexOf('c', -5); // returns 0(指明 fromIndex 为-5 则视同 0,从 c 处向左回向查找发现自己就是,故返回 0) 'canal'.lastIndexOf('c', 0); // returns 0(指明 fromIndex 为 0 则从 c 处向左回向查找 c 发现自己就是,故返回自己的索引 0) 'canal'.lastIndexOf(''); // returns 5 'canal'.lastIndexOf('', 2); // returns 2

6、startsWith()

用来判断当前字符串是否以另外一个给定的子字符串开头,并根据判断结果返回 true 或 false

```JavaScript const str1 = 'Saturday night plans';

console.log(str1.startsWith('Sat')); // expected output: true

console.log(str1.startsWith('Sat', 3)); // expected output: false ```

7、endsWith()

用来判断当前字符串是否是以另外一个给定的子字符串“结尾”的,根据判断结果返回 true 或 false

```JavaScript var str = "To be, or not to be, that is the question.";

alert( str.endsWith("question.") ); // true alert( str.endsWith("to be") ); // false alert( str.endsWith("to be", 19) ); // true ```

8、includes()

用于判断一个字符串是否包含在另一个字符串中,根据情况返回 true 或 false

```JavaScript var str = 'To be, or not to be, that is the question.';

console.log(str.includes('To be')); // true console.log(str.includes('question')); // true console.log(str.includes('nonexistent')); // false console.log(str.includes('To be', 1)); // false console.log(str.includes('TO BE')); // false ```

总结一下

上面介绍的8个方法,大致分为三类 - 字符串指定位置的字符或者字符编码:charAt, charCodeAt, codePointAt - 返回字符串中指定子串的位置或最后位置:indexOf, lastIndexOf - 字符串是否以指定字符串开始、结束或包含指定字符串:startsWith, endsWith, includes

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值