[JavaScript] String 方法总结

字符方法(3)

charAt(pos: number): string;
// 返回特定位置的字符。
charCodeAt(index: number): number;
// 返回表示给定索引的字符的Unicode的值。
codePointAt(pos: number): number | undefined;
// 返回使用UTF-16编码的给定位置的值的非负整数。

是否满足(3)

startsWith(searchString: string, position?: number): boolean;
// 判断字符串的起始位置是否匹配其他字符串中的字符。
endsWith(searchString: string, endPosition?: number): boolean;
// 判断一个字符串的是否以给定字符串结尾,结果返回布尔值。
includes(searchString: string, position?: number): boolean;
// 判断一个字符串里是否包含其他字符串。

找下标(2)

indexOf(searchString: string, position?: number): number;
// 从字符串对象中返回首个被发现的给定值的索引值,如果没有找到则返回 - 1。
lastIndexOf(searchString: string, position?: number): number;
// 从字符串对象中返回最后一个被发现的给定值的索引值,如果没有找到则返回 - 1。

正则(3)

search(regexp: string | RegExp): number;
// 对正则表达式和指定字符串进行匹配搜索,返回第一个出现的匹配项的下标。
match(regexp: string | RegExp): RegExpMatchArray | null;
// 使用正则表达式与字符串相比较。
replace(searchValue: string | RegExp, replaceValue: string): string;
replace(searchValue: string | RegExp, replacer: (substring: string, ...args: any[]) => string): string;
// 被用来在正则表达式和字符串直接比较,然后用新的子串来替换被匹配的子串。

扩充(4)

String.prototype.padEnd()
// 在当前字符串尾部填充指定的字符串, 直到达到指定的长度。 返回一个新的字符串。
String.prototype.padStart()
// 在当前字符串头部填充指定的字符串, 直到达到指定的长度。 返回一个新的字符串。
String.prototype.repeat()
// 返回指定重复次数的由元素组成的字符串对象。
String.prototype.concat()
// 连接两个字符串文本,并返回一个新的字符串。

截取(3)

slice(start?: number, end?: number): string;
// 摘取一个字符串区域,返回一个新的字符串。
substr(from: number, length?: number): string;
// 通过指定字符数返回在指定位置开始的字符串中的字符。
substring(start: number, end?: number): string;
// 返回在字符串中指定两个下标之间的字符。

数组(1)

split(separator: string | RegExp, limit?: number): string[];
// 通过分离字符串成字串,将字符串对象分割成字符串数组。

大小写转换(4)

toLocaleLowerCase(locales?: string | string[]): string;
// 根据当前区域设置,将符串中的字符转换成小写。对于大多数语言来说,toLowerCase的返回值是一致的。
toLocaleUpperCase(locales?: string | string[]): string;
// 根据当前区域设置,将字符串中的字符转换成大写,对于大多数语言来说,toUpperCase的返回值是一致的。
toLowerCase(): string;
// 将字符串转换成小写并返回。
toUpperCase(): string;
// 将字符串转换成大写并返回。

去空格(5)

trim(): string;
// 从字符串的开始和结尾去除空格。参照部分 ECMAScript 5 标准。
trimStart(): string;
trimLeft(): string;
// 从字符串的左侧去除空格。
trimEnd(): string;
trimRight(): string;
// 从字符串的右侧去除空格。

其他(4)

toString(): string;
// 返回用字符串表示的特定对象。重写 Object.prototype.toString 方法。
valueOf(): string;
// 返回特定对象的原始值。重写 Object.prototype.valueOf 方法。
localeCompare(that: string, locales?: string | string[], options?: Intl.CollatorOptions): number;
// 返回一个数字表示是否引用字符串在排序中位于比较字符串的前面,后面,或者二者相同。
normalize(form?: string): string;
// 返回调用字符串值的Unicode标准化形式。
原文地址:https://www.cnblogs.com/whosmeya/p/12450855.html