JavaScript中23个String方法(中)

简单介绍

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

方法介绍(中)

9、concat()

将一个或多个字符串与原字符串连接合并,形成一个新的字符串并返回。

```JavaScript let hello = 'Hello, ' console.log(hello.concat('Kevin', '. Have a nice day.')) // Hello, Kevin. Have a nice day.

let greetList = ['Hello', ' ', 'Venkat', '!'] "".concat(...greetList) // "Hello Venkat!"

"".concat({}) // [object Object] "".concat([]) // "" "".concat(null) // "null" "".concat(true) // "true" "".concat(4, 5) // "45" ```

10、fromCharCode()

静态 String.fromCharCode() 方法返回由指定的 UTF-16 代码单元序列创建的字符串。

JavaScript String.fromCharCode(65, 66, 67); // 返回 "ABC" String.fromCharCode(0x2014); // 返回 "—" String.fromCharCode(0x12014); // 也是返回 "—"; 数字 1 被剔除并忽略 String.fromCharCode(8212); // 也是返回 "—"; 8212 是 0x2014 的十进制表示

11、fromCodePoint()

String.fromCodePoint() 静态方法返回使用指定的代码点序列创建的字符串。 ```JavaScript String.fromCodePoint(42); // "*" String.fromCodePoint(65, 90); // "AZ" String.fromCodePoint(0x404); // "\u0404" String.fromCodePoint(0x2F804); // "\uD87E\uDC04" String.fromCodePoint(194564); // "\uD87E\uDC04" String.fromCodePoint(0x1D306, 0x61, 0x1D307) // "\uD834\uDF06a\uD834\uDF07"

String.fromCodePoint('_'); // RangeError String.fromCodePoint(Infinity); // RangeError String.fromCodePoint(-1); // RangeError String.fromCodePoint(3.14); // RangeError String.fromCodePoint(3e-2); // RangeError String.fromCodePoint(NaN); // RangeError ```

12、split()

使用指定的分隔符字符串将一个String对象分割成子字符串数组,以一个指定的分割字串来决定每个拆分的位置。

```JavaScript const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split(' '); console.log(words[3]); // expected output: "fox"

const chars = str.split(''); console.log(chars[8]); // expected output: "k"

const strCopy = str.split(); console.log(strCopy); // expected output: Array ["The quick brown fox jumps over the lazy dog."]

```

13、slice()

提取某个字符串的一部分,并返回一个新的字符串,且不会改动原字符串

```JavaScript const str = 'The quick brown fox jumps over the lazy dog.';

console.log(str.slice(31)); // expected output: "the lazy dog."

console.log(str.slice(4, 19)); // expected output: "quick brown fox"

console.log(str.slice(-4)); // expected output: "dog."

console.log(str.slice(-9, -5)); // expected output: "lazy" ```

14、substring()

返回一个字符串在开始索引到结束索引之间的一个子集,或从开始索引直到字符串的末尾的一个子集。

```JavaScript var anyString = "Mozilla";

// 输出 "Moz" console.log(anyString.substring(0,3)); console.log(anyString.substring(3,0)); console.log(anyString.substring(3,-3)); console.log(anyString.substring(3,NaN)); console.log(anyString.substring(-2,3)); console.log(anyString.substring(NaN,3));

// 输出 "lla" console.log(anyString.substring(4,7)); console.log(anyString.substring(7,4));

// 输出 "" console.log(anyString.substring(4,4));

// 输出 "Mozill" console.log(anyString.substring(0,6));

// 输出 "Mozilla" console.log(anyString.substring(0,7)); console.log(anyString.substring(0,10));

```

15、substr()

返回一个字符串中从指定位置开始到指定字符数的字符(注意:该方法可能会被废弃,使用substring代替)

```JavaScript var str = "abcdefghij";

console.log("(1,2): " + str.substr(1,2)); // (1,2): bc console.log("(-3,2): " + str.substr(-3,2)); // (-3,2): hi console.log("(-3): " + str.substr(-3)); // (-3): hij console.log("(1): " + str.substr(1)); // (1): bcdefghij console.log("(-20, 2): " + str.substr(-20,2)); // (-20, 2): ab console.log("(20, 2): " + str.substr(20,2)); // (20, 2):

```

汇总一下

  • concat:连接两个字符串并返回新的字符串。
  • fromCharCode, fromCodePoint:从指定的 Unicode 值序列构造一个字符串。这是一个 String 类方法,不是实例方法。
  • split:通过将字符串分离成一个个子串来把一个 String 对象分裂到一个字符串数组中。
  • slice:从一个字符串提取片段并作为新字符串返回。
  • substring, substr:分别通过指定起始和结束位置,起始位置和长度来返回字符串的指定子集。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值