String對象方法詳記

測試demo

String

  1. charAt
/**
 * 返回指定位置的字符
 * @param pos
 */
let str = 'this is string';

console.log(str.charAt(2)); // i
  1. charCodeAt
/**
 * 返回指定位置的字符的Unicode 編碼
 * @param index
 */
let str = 'this is string';
console.log(str.charCodeAt(2)); // 105
  1. codePointAt
/**
 * 返回指定位置的字符的 Unicode utf-16 字符的碼位
 * @param pos
 */
let str = 'this is string';
console.log(str.codePointAt(2)) // 105
  1. 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
  1. endsWith
/**
 * endsWith(searchString: string, endPosition?: number) : boolean
 * 是否是指定字符結(jié)尾,可指定結(jié)束搜索的位置,返回布爾值
 */
let str = "hello";
console.log(str.endsWith("e")); //false
console.log(str.endsWith("o")); //true
  1. includes
/**
 * 是否包含指定的子字符串
 * @param searchString
 * @param position 默認0,開始搜索的索引
 */
let str = 'hello';
console.log(str.includes('el',3)) //false
console.log(str.includes('el',1)) //true
  1. 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
  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
  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' ]
  1. repeat
    不改變原字符串
/**
 * 返回復(fù)制指定次數(shù)并連接在一起的字符串
 * @param count number of copies to append
 */
let str = 'hello';
console.log(str.repeat(2)) // hellohello
console.log(str) // hello
  1. 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
  1. 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
  1. slice
    返回新字符串,不改變原字符串
/**
 * slice(start?: number, end?: number): string
 * 提取字符串的片斷,并在新的字符串中返回被提取的部分
 */
let str = 'hello this is slice';
let sstr = str.slice(2,5);
console.log(sstr) // llo
  1. 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' ]
  1. startsWith
/**
 * startsWith(searchString: string, position?: number): boolean
 * 檢測字符串是否以指定的子字符串開始
 */
let str = 'this is startsWith';
console.log(str.startsWith('th')) // true
console.log(str.startsWith('t1')) // false
  1. 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
  1. 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
  1. 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
  1. 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 
  1. valueOf
/**
 * 輸出String 對象的原始值
 */
let str = 'hello';
console.log(str.valueOf()) // hello
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容