String

参考

方法

String构造器方法

String.prototype

属性

String.prototype共有两个属性,如下:

  • String.prototype.constructor 指向构造器(String())
  • String.prototype.length 表示字符串长度

fromCharCode

静态 String.fromCharCode() 方法返回使用指定的Unicode值序列创建的字符串

1
String.fromCharCode(65,66,67) // "ABC"

fromCodePoint

String.fromCodePoint() 静态方法返回使用指定的代码点序列创建的字符串,该方法主要扩展了对4字节字符的支持

1
2
3
4
5
6
7
8
9
10
11
12
13
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

raw

String.raw() 是一个模板字符串的标签函数,它的作用类似于 Python 中的字符串前缀 r 和 C# 中的字符串前缀 @,是用来获取一个模板字符串的原始字面量值的。

charAt

返回特定位置的字符。

1
2
3
4
5
6
var anyString = "Brave new world";
anyString.charAt(0) // 'B'
anyString.charAt(1) // 'r'
anyString.charAt(2) // 'a'
anyString.charAt(3) // 'v'
anyString.charAt(4) // 'e'

charCodeAt

返回表示给定索引的字符的Unicode的值。

codePointAt

返回使用UTF-16编码的给定位置的值的非负整数。

concat

连接两个字符串文本,并返回一个新的字符串。

includes

判断一个字符串里是否包含其他字符串。

endsWith

判断一个字符串的结尾是否包含其他字符串中的字符。

String.prototype.indexOf()

从字符串对象中返回首个被发现的给定值的索引值,如果没有找到则返回-1。

String.prototype.lastIndexOf()

从字符串对象中返回最后一个被发现的给定值的索引值,如果没有找到则返回-1。

String.prototype.localeCompare()

返回一个数字表示是否引用字符串在排序中位于比较字符串的前面,后面,或者二者相同。

String.prototype.match()

使用正则表达式与字符串相比较。

1
2
3
var str = "World Internet Conference";
console.log(str.match(/[a-d]/i)); // ["d", index: 4, input: "World Internet Conference"]
console.log(str.match(/[a-d]/gi)); // ["d", "C", "c"]

codePointAt

返回调用字符串值的Unicode标准化形式。

String.prototype.padEnd()

在当前字符串尾部填充指定的字符串, 直到达到指定的长度。 返回一个新的字符串。

String.prototype.padStart()

在当前字符串头部填充指定的字符串, 直到达到指定的长度。 返回一个新的字符串。

repeat

返回指定重复次数的由元素组成的字符串对象。

replace

被用来在正则表达式和字符串直接比较,然后用新的子串来替换被匹配的子串。

对正则表达式和指定字符串进行匹配搜索,返回第一个出现的匹配项的下标。

1
2
var str = "abcdefg";
console.log(str.search(/[d-g]/)); // 3, 匹配到子串"defg",而d在原字符串中的索引为3

slice

摘取一个字符串区域,返回一个新的字符串。

split

通过分离字符串成字串,将字符串对象分割成字符串数组。

startsWith

判断字符串的起始位置是否匹配其他字符串中的字符。

substr

通过指定字符数返回在指定位置开始的字符串中的字符。


String.prototype.substr

str.substr(start[, length])
1
2
3
4
5
6
7
8
9
10
var str = "abcdefghij";
var str = "abcdefghij";

str.substr(1,2); // (1,2): bc
str.substr(-3,2); // (-3,2): hi
str.substr(-3); // (-3): hij
str.substr(1); // (1): bcdefghij
str.substr(20,2); // (20, 2):
// start 为负值且 abs(start) 大于字符串的长度,则 substr 使用 0 作为开始提取的索引
str.substr(-20,2); // (-20, 2): ab


substring

返回在字符串中指定两个下标之间的字符。


String.prototype.substring

str.substring(indexStart[, indexEnd])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
跟slice方法很相像
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));


toLocaleLowerCase

根据当前区域设置,将符串中的字符转换成小写。对于大多数语言来说,toLowerCase的返回值是一致的。

toLocaleUpperCase

根据当前区域设置,将字符串中的字符转换成大写,对于大多数语言来说,toUpperCase的返回值是一致的。

toLowerCase

将字符串转换成小写并返回。

String.prototype.toSource()

返回一个对象文字代表着特定的对象。你可以使用这个返回值来创建新的对象。重写 Object.prototype.toSource 方法。

String.prototype.toString()

返回用字符串表示的特定对象。重写 Object.prototype.toString 方法。

toUpperCase

将字符串转换成大写并返回。

trim

从字符串的开始和结尾去除空格。参照部分 ECMAScript 5 标准。

String.prototype.trimLeft()

从字符串的左侧去除空格。

String.prototype.trimRight()

从字符串的右侧去除空格。

String.prototype.valueOf()

返回特定对象的原始值。重写 Object.prototype.valueOf 方法。

String.prototype@@iterator

返回一个新的迭代器对象,该对象遍历字符串值的索引位置,将每个索引值作为字符串值返回。


⬆ Back to top