js的String物件方法使用

2021-10-19 18:19:45 字數 4268 閱讀 4980

1、tolowercase(): 把字串轉為小寫,返回新的字串。

var str=

"hello world"

;var str1=str.

tolowercase()

;console.

log(str)

;//hello world

console.

log(str1)

;//hello world

2、touppercase(): 把字串轉為大寫,返回新的字串。

var str=

"hello world"

;var str1=str.

touppercase()

;console.

log(str)

;//hello world

console.

log(str1)

;//hello world

3、charat(): 返回指定下標位置的字元。如果index不在0-str.length(不包含str.length)之間,返回空字串。

var str=

"hello world"

;var str1=str.

charat(6

);console.

log(str1)

;//w

4、indexof(): 返回某個指定的子字串在字串中第一次出現的位置

var str=

"hello world"

;var str1=str.

indexof

("o");

var str2=str.

indexof

("world");

var str3=str.

indexof

("o"

,str1+1)

;console.

log(str1)

;//2 預設只找第乙個關鍵字位置,從下標0開始查詢

console.

log(str2)

;//-1

console.

log(str3)

;//7

注意:indexof()方法對大小寫敏感,如果子字串沒有找到,返回-1。第二個引數表示從哪個下標開始查詢,沒有寫則預設從下標0開始查詢。

5、lastindexof(): 返回某個指定的子字串在字串中最後出現的位置。

var str=

"hello world"

;var str1=str.

lastindexof

("o");

var str2=str.

lastindexof

("world");

var str3=str.

lastindexof

("o"

,str1-1)

;console.

log(str1)

;//7

console.

log(str2)

;//-1

console.

log(str3)

;//4

6、slice(): 返回字串中提取的子字串。

var str=

"hello world"

;var str1=str.

slice(2

);//如果只有乙個引數,則提取開始下標到結尾處的所有字串

var str2=str.

slice(2

,7);

//兩個引數,提取下標為2,到下標為7但不包含下標為7的字串

var str3=str.

slice(-

7,-2

);//如果是負數,-1為字串的最後乙個字元。提取從下標-7開始到下標-2但不包含下標-2的字串。前乙個數要小於後乙個數,否則返回空字串

console.

log(str1)

;//llo world

console.

log(str2)

;//llo w

console.

log(str3)

;//o wor

7、substring(): 提取字串中介於兩個指定下標之間的字元。

var str=

"hello world"

;var str1=str.

substring(2

)var str2=str.

substring(2

,2);

var str3=str.

substring(2

,7);

console.

log(str1)

;//llo world

console.

log(str2)

;//如果兩個引數相等,返回長度為0的空串

console.

log(str3)

;//llo w

8、substr(): 返回從指定下標開始指定長度的的子字串

var str=

"hello world"

;var str1=str.

substr(1

)var str2=str.

substr(1

,3);

var str3=str.

substr(-

3,2)

;console.

log(str1)

;//ello world

console.

log(str2)

;//ell

console.

log(str3)

;//rl

9、split(): 把字串分割成字串陣列。

var str=

"aa bb cc dd"

;var string1=

"1:2:3:4:5"

;var str1=str.

split(""

);//如果把空字串 ("")用作分割符,那麼字串的每個字元之間都會被分割

var str2=str.

split

(" ");

//以空格為分隔符

var str3=str.

split(""

,4);

//4指定返回陣列的最大長度

var str4=string1.

split

(":");

console.

log(str1)

;// ["a", "a", " ", "b", "b", " ", "c", "c", " ", "d", "d"]

console.

log(str2)

;//["aa" "bb" "cc" "dd"]

console.

log(str3)

;//["a", "a", " ", "b"]

console.

log(str4)

;// ["1", "2", "3", "4", "5"]

10、replace(): 在字串中用一些字元替換另一些字元,或替換乙個與正規表示式匹配的子串。

var str=

"hello world"

;var reg=

/o/ig

;//o為要替換的關鍵字,不能加引號,否則替換不生效,i忽略大小寫,g表示全域性查詢。

var str1=str.

replace

(reg,

"**"

)console.

log(str1)

;//hell** w**rld

11、match(): 返回所有查詢的關鍵字內容的陣列。

var str=

"to be or not to be"

;var reg=

/to/ig

;var str1=str.

match

(reg)

;console.

log(str1)

;//["to", "to"]

console.

log(str.

match

("hello"))

;//null

js的String物件方法

1 tolowercase 把字串轉為小寫,返回新的字串。var str hello world var str1 str.tolowercase console.log str hello world console.log str1 hello world2 touppercase 把字串轉為大...

JS操作String物件的方法

js操作string物件的方法 charat index 返回指定索引處的字串 charcodeat index 返回指定索引處的字元的unicode的值 concat str1,str2,連線多個字串,返回連線後的字串的副本 fromcharcode 將unicode值轉換成實際的字串 index...

JS中String物件的常用方法

1 charcodeat方法返回乙個整數,代表指定位置字元的unicode編碼。strobj.charcodeat index 說明 index將被處理字元的從零開始計數的編號。有效值為0到字串長度減1的數字。如果指定位置沒有字元,將返回nan。例如 var str abc str.charcode...