JS中字串方法

2021-09-19 16:35:41 字數 1546 閱讀 5500

lang

="en"

>

>

charset

="utf-8"

>

>

字串方法title

>

head

>

>

>

var str =

"王hello world"

;var str1 =

newstring

("0123456789");

console.

log(str.length)

; console.

log(str.

charat(1

));//查詢索引為1的位置的字母

console.

log(str.

charcodeat(0

));//查詢索引為0的位置的unicode編碼

console.

log(str.

replace

('王'

,'wangsibo'))

;//查字串中的「王」替換成「wangsibo」

console.

log(str1.

slice()

);//乙個引數不傳,擷取整個字串 結果:0123456789

console.

log(str1.

slice(0

));//從索引為0的字元,擷取到最後 結果:0123456789

console.

log(str1.

slice(0

,6))

;//字串擷取前包後不包 結果:012345

console.

log(str1.

slice(-

5));

//結果:56789

//substring使用方式和slice幾乎相同

//substr(擷取的開始位置,擷取的個數)

console.

log(str1.

substr(0

,6))

;//結果:56789

console.

log(str.

split

(' '))

;// 結果:["王hello", "world"]

console.

log(str.

touppercase()

);//轉換大寫字母 結果:王hello world

console.

log(str.

tolowercase()

);//轉換小寫字母 結果:王hello world

//獲取字串的位元組長度,假設英文為1個位元組,中文為兩個位元組

function

ischinese

(str)

} }ischinese

(str)

script

>

body

>

html

>

js中字串的方法

1.charat 返回指定索引出的字元 var str abcd var a str.charat 0 console.log a a console.log str abcd 2.charcodeat 返回指定索引出的unicode字元 str.charcodeat 0 97 3.indexof ...

JS中字串常用方法

js中字串常用方法 1 tolowercase 把字串轉為小寫,返回新的字串。var str hello world var str1 str.tolowercase console.log str1 hello world 2 touppercase 把字串轉為大寫,返回新的字串。var str ...

JS中的字串方法

string型別 string型別是字串的物件包裝型別 字串的length方法 1.字元方法 charat charcodeat 2 字串操作方法 字串的拼接 concat 方法,一般用 號拼接字串 還有三個擷取字串的方法,slice substring substr 當傳入的引數是正數的時候 sl...