简单介绍
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:分别通过指定起始和结束位置,起始位置和长度来返回字符串的指定子集。