js中一些常見的字串方法

2021-10-14 03:57:43 字數 1405 閱讀 5062

let str =

'djhdgahj'

let str1 =

'dhgdka'

// tolowercase 轉換為小寫

console.

log(str.

tolowercase()

);// djhdgahj

// touppercase 轉換為大寫

console.

log(str1.

touppercase()

);// dhgdka

// charat 返回指定下標的字元

console.

log(str1.

charat(3

));// d

// substr 從下標為2開始擷取3個字元

console.

log(str1.

substr(2

,3))

;// gdk

// substring 擷取下標為2和3之間的字元

console.

log(str1.

substring(2

,3))

;// g

// split 字串轉為陣列

console.

log(str1.

split()

);// ["dhgdka"]

console.

log(str1.

split(''

));// ["d", "h", "g", "d", "k", "a"]

// concat 字串連線

console.

log(str1.

concat

(str));

// dhgdkadjhdgahj

// indexof 返回字元在字串中的索引 找不到返回-1

console.

log(str1.

indexof

('g'))

;// 2

// replace 替換字串中的字元(只替換第乙個)

console.

log(str1.

replace

('g'

,'z'))

;// dhzdka

// replace 替換字串中的所有d字元

console.

log(str1.

replace

(/d/g

,'c'))

;// chgcka

// slice 抽取子串

console.

log(str1.

slice(1

,3))

;// hg

<

/script>

js 中字串的一些方法

var txt 這是乙個字串 console.log typeof txt string string 物件方法 length 字串的長度 charat 返回在指定位置的字元。charcodeat 返回在指定的位置的字元的 unicode 編碼。concat 連線兩個或更多字串,並返回新的字串。fr...

C 中一些常見的方法

1.對規則的字串進行處理的bool splitstring string strorigin,string strsplit,vector vct string str strorigin.substr 0,iindex vct string.push back str ilen int stror...

Python 字串中常見的一些方法

str.capitalize 將字串的第乙個字母變成大寫,其他字母變小寫。str this is string example print str.capitalize 結果 this is string example str.title 所有單詞的首個字母轉化為大寫,其餘字母均為小寫。str t...