String
参考
方法
fromCharCodefromCodePoint(ES6)raw(ES6)charAtcharCodeAtconcatindexOflastIndexOflocaleComparematchreplacesearchslicesplitsubstrsubstringtoLocaleLowerCasetoLocaleUpperCasetoLowerCasetoUpperCasetoStringvalueOftrimcodePointAt(ES6)includes(ES6)endsWith(ES6)normalize(ES6)repeat(ES6)startsWith(ES6)HTML有关的方法anchorlink
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
13String.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
6var 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
3var 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
被用来在正则表达式和字符串直接比较,然后用新的子串来替换被匹配的子串。
search
对正则表达式和指定字符串进行匹配搜索,返回第一个出现的匹配项的下标。1
2var str = "abcdefg";
console.log(str.search(/[d-g]/)); // 3, 匹配到子串"defg",而d在原字符串中的索引为3
slice
摘取一个字符串区域,返回一个新的字符串。
split
通过分离字符串成字串,将字符串对象分割成字符串数组。
startsWith
判断字符串的起始位置是否匹配其他字符串中的字符。
substr
通过指定字符数返回在指定位置开始的字符串中的字符。
String.prototype.substr
str.substr(start[, length])
1 | var str = "abcdefghij"; |
substring
返回在字符串中指定两个下标之间的字符。
String.prototype.substring
str.substring(indexStart[, indexEnd])1 | 跟slice方法很相像 |
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
返回一个新的迭代器对象,该对象遍历字符串值的索引位置,将每个索引值作为字符串值返回。