js 字串相關方法整理

2021-08-28 11:46:44 字數 3562 閱讀 4518

一.字串切割與提取

1.slice(start,end);兩個引數可正可負,負值代表從右擷取

var mystr="hello world!";

var slicestr1=mystr.slice(-3); //ld!

var slicestr2=mystr.slice(-3,-1); //ld

var slicestr3=mystr.slice(3);//lo world!

var slicestr4=mystr.slice(3,7); //lo w

2.substring(start,end);兩個引數都為正數

var mystr="hello world!"

var slicestr1=mystr.substring(3); //lo world!

var slicestr2=mystr.substring(3,7); //lo w

3.substr(start,length);start引數可正可負,負數代表從右擷取,length為擷取長度

var mystrempty = ' hello world!'

console.log(mystrempty.substr(4));//lo world!

console.log(mystrempty.substr(5,3));//o w

二.其它方法

4.charat(index);返回子字串,index為字串下標

var mystr="hello world!";

console.log(mystr.charat(4));//o

5.charcodeat(index);返回子字串的unicode編碼

var mystr="hello world!";

console.log(mystr.charcodeat(4));//111

6.fromcharcode(num1,num2);靜態方法,根據unicode編碼返回字串

console.log(string.fromcharcode(111))//o
7.indexof(searchstring,startindex);返回子字串第一次出現的位置,從startindex開始查詢,找不到時返回-1

var mystr="hello world!";

console.log(mystr.indexof("o",5));//7

console.log(mystr.indexof("o"));//7

console.log(mystr.indexof("x"));//-1

8.lastindexof(searchstring,startindex);從右往左找子字串,找不到時返回-1

var mystr="hello world!";

console.log(mystr.lastindexof("x"));//-1

console.log(mystr.lastindexof("d"));//10

console.log(mystr.lastindexof("d",2));//-1

9.split(separator,limit);字串分割成陣列,引數1指定字串或正則,引數2指定陣列的最大長度

var mystr="hello world!";

console.log(mystr.split());//["hello world!"] 整個字串放到陣列中

console.log(mystr.split(''));//["h", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d", "!"]

console.log(mystr.split('l'));//["he", "", "o wor", "d!"]

console.log(mystr.split('l',2));// ["he", ""]

10.match(pattern);  正則匹配,字串或者正規表示式

var mystr="hello world!";

console.log(mystr.match(/l/));//["l", index: 2, input: "hello world!", groups: undefined]

console.log(mystr.match(/d/));//["d", index: 10, input: "hello world!", groups: undefined]

console.log(mystr.match(/x/));//null

console.log(mystr.match(/l/g));//["l", "l", "l"]

console.log(mystr.match(/d/g));//["d"]

console.log(mystr.match(/x/g));//null

console.log(mystr.match("l"));//["l", index: 2, input: "hello world!", groups: undefined]

11.search(pattern);返回值是匹配項的索引,如果沒有找到匹配的子字串,則返回-1

var mystr="hello world!";

console.log(mystr.search(/l/));//2

console.log(mystr.search(/l/g));//2

console.log(mystr.search("l"));//2

console.log(mystr.search("x"));//-1

12.replace(rgexp/substr,replacetext),返回替換後的字串

var mystr="hello world!";

console.log(mystr.replace("l","5"));//he5lo world! 只替換第乙個l

console.log(mystr.replace(/l/g,"5"));//he55o wor5d! 替換所有l

13.trim(); 刪除字串頭和尾部的空格

var mystrempty = ' hello world!'

console.log(mystrempty.trim());//hello world! 刪除字串頭和尾部的空格

擴充套件:去除字串內所有空格

function(str)
14.tolowercase(),touppercase();大小寫

var mystrempty = ' hello world!'

console.log(mystrempty.tolowercase());//" hello world!"

console.log(mystrempty.touppercase());//" hello world!"

js字串相關方法

var wordstr hello,world,fine 返回給定位置的字元 wordstr.charat 2 返回l 返回給定位置字元的字元編碼 wordstr.charcodeat 2 返回l的字串編碼108 擷取子字串 wordstr.substr start,length 從字串的開始下標,...

js字串常用方法詳細整理

1 concat 合併字串 var a hello var b world var c a.concat b console.log c c hello world 2 indexof 返回字串中的字元出現的位置 從左到右搜尋 1 var a hello var b a.indexof l cons...

字串相關函式整理

字串逆轉函式 1 可用標頭檔案裡的reverse函式,函式使用方法 string str reverse str.begin str.end 2 使用標頭檔案中的strrev函式,函式使用方法 string str strrev str 讀取字串 1 cin 2 cin.get 使用方法一 只能讀取...