
String
- charAt
/**
* 返回指定位置的字符
* @param pos
*/
let str = 'this is string';
console.log(str.charAt(2)); // i
- charCodeAt
/**
* 返回指定位置的字符的Unicode 編碼
* @param index
*/
let str = 'this is string';
console.log(str.charCodeAt(2)); // 105
- codePointAt
/**
* 返回指定位置的字符的 Unicode utf-16 字符的碼位
* @param pos
*/
let str = 'this is string';
console.log(str.codePointAt(2)) // 105
- concat
/**
* 返回連接后的新字符串
* @param strings
*/
let str = "hello";
let str2 = str.concat(",world");
let str3 = str.concat("_", "world");
console.log(str2); // hello,world
console.log(str3); // hello_world
- endsWith
/**
* endsWith(searchString: string, endPosition?: number) : boolean
* 是否是指定字符結(jié)尾,可指定結(jié)束搜索的位置,返回布爾值
*/
let str = "hello";
console.log(str.endsWith("e")); //false
console.log(str.endsWith("o")); //true
- includes
/**
* 是否包含指定的子字符串
* @param searchString
* @param position 默認0,開始搜索的索引
*/
let str = 'hello';
console.log(str.includes('el',3)) //false
console.log(str.includes('el',1)) //true
- indexOf
/**
* indexOf(searchString: string, position?: number): number
* 返回某個指定的字符串值在字符串中首次出現(xiàn)的位置索引
*/
let str = 'hello world'
console.log(str.indexOf('o')) // 4
console.log(str.indexOf('o', 5)) // 7
console.log(str.indexOf('m')) // -1
- lastIndexOf
/**
* lastIndexOf(searchString: string, position?: number): number
* 返回某個指定的字符串值在字符串中首次出現(xiàn)的位置索引,從右往左開始查找
*/
let str = 'hello world'
console.log(str.lastIndexOf('o')) // 7
console.log(str.lastIndexOf('o', 5)) // 4
console.log(str.lastIndexOf('m')) // -1
- match
/**
* @param matcher An object that supports being matched against
* 返回存放匹配結(jié)果的數(shù)組
*/
let str = "hello world";
let mat = str.match("he");
console.log(mat); // [ 'he', index: 0, input: 'hello world' ]
let str2 = 'hello, he is my girlfriend';
let reg = str2.match(/he/g)
let reg2 = str2.match(/\w{5}/g)
console.log(reg) //[ 'he', 'he' ]
console.log(reg2) //[ 'hello', 'girlf', 'riend' ]
- repeat
不改變原字符串
/**
* 返回復(fù)制指定次數(shù)并連接在一起的字符串
* @param count number of copies to append
*/
let str = 'hello';
console.log(str.repeat(2)) // hellohello
console.log(str) // hello
- replace
/**
* 使用指定字符替換原有指定字符
* @param searchValue 搜索的子字符串或reg對象
* @param replacer 替換的文本或者返回替換文本的函數(shù)
*/
let str = 'hello world, this is my string';
let str1 = str.replace('string', 'world');
console.log(str) // hello world, this is my string
console.log(str1) // hello world, this is my world
let str2 = str.replace(/\s/g, '_');
console.log(str2) // hello_world,_this_is_my_string
- search
/**
* 用于檢索字符串中指定的子字符串,或檢索與正則表達式相匹配的子字符串,沒有就返回 -1
* @param searcher
*/
let str = 'hello this is search';
let str1 = str.search('this')
let str2 = str.search(/is/g)
console.log(str1) // 6
console.log(str2) // 8
- slice
返回新字符串,不改變原字符串
/**
* slice(start?: number, end?: number): string
* 提取字符串的片斷,并在新的字符串中返回被提取的部分
*/
let str = 'hello this is slice';
let sstr = str.slice(2,5);
console.log(sstr) // llo
- split
/**
* string.split(separator,limit?: number)
* @param separator 字符串或正則表達式,從該參數(shù)指定的地方分割
* @param limit 可選。該參數(shù)可指定返回的數(shù)組的最大長度。
*/
let str = "this is split";
let strArr = str.split(" ");
let strArr1 = str.split(" ", 2);
console.log(strArr); // [ 'this', 'is', 'split' ]
console.log(strArr1); // [ 'this', 'is' ]
- startsWith
/**
* startsWith(searchString: string, position?: number): boolean
* 檢測字符串是否以指定的子字符串開始
*/
let str = 'this is startsWith';
console.log(str.startsWith('th')) // true
console.log(str.startsWith('t1')) // false
- substr
返回新字符串,不改變原字符串。第二個參數(shù)是長度
/**
* substr(from: number, length?: number): string;
* 在字符串中抽取從 開始 下標開始的指定數(shù)目的字符, 返回一個新的字符串
*/
let str = "this is substr";
let str1 = str.substr(2, 5);
console.log(str); // this is substr
console.log(str1); // is is
- substring
第二個參數(shù)非負
/**
* substring(start: number, end?: number): string;)
* @param end 非負的整數(shù),省略該參數(shù),那么返回的子串會一直到字符串的結(jié)尾
*/
let str = "this is substring";
let str1 = str.substring(2, 3);
let str2 = str.substring(2);
console.log(str); //this is substring
console.log(str1); // i
console.log(str2); // is is substring
- toLowerCase toUpperCase
/**
* toLowerCase 轉(zhuǎn)小寫
* toUpperCase 轉(zhuǎn)大寫
*/
let str = 'This is toLowerCase toUpperCase';
console.log(str.toLowerCase()) // this is tolowercase touppercase
console.log(str.toUpperCase()) // THIS IS TOLOWERCASE TOUPPERCASE
- trim
/**
* 去除兩邊空白,不改變原字符串, 也可使用Left,Start,End,Right去除任意一邊的空白
*/
let str = ' this is he ';
console.log(str.trim()) //this is he
console.log(str.trimEnd()) // this is he
console.log(str.trimLeft()) //this is he
console.log(str.trimRight()) // this is he
console.log(str.trimStart()) //this is he
- valueOf
/**
* 輸出String 對象的原始值
*/
let str = 'hello';
console.log(str.valueOf()) // hello