String型別的屬性和方法

2021-07-13 22:39:04 字數 4189 閱讀 6787

字串string型別的每個例項都有乙個length屬性,表示字串中的字元個數。由於字串是不可變的,所以字串的長度也不可變

字串的length屬性不會在for/in迴圈中列舉,也不能通過delete操作符刪除

[注意]對於字串s來說,最後乙個字元的索引是s.length - 1

var str = "test";

console.log(str.length);//4

str.length = 6;

console.log(str,str.length);//"test",4

例項方法

字串string物件多達20多個例項方法,包括tostring()、tolocalestring()、valueof()從object物件繼承的3種物件通用方法,chartat()、中括號、charcodeat()和fromcharcode()4種訪問字元方法,concat()和加號+這2種字串拼接方法,slice()、substr()和substring()3種建立子字串方法,tolowercase()、tolocalelowercase()、touppercase()、tolocaleuppercase()這4種大小寫轉換方法,indexof()和lastindexof()這2種查詢字串位置的方法,match()、search()、replace()、split()這4種正則匹配方法以及去除首尾空格的trim()方法和字串比較的localecompare()方法

string型別是與字串對應的包裝型別,繼承了object物件的通用方法tostring()、tolocalestring()、valueof()這三個方法

【tostring()】

tostring()方法返回string的原始字串值

【tolocalestring()】

tolocalestring()方法返回string的原始字串值

【valueof()】

valueof()方法返回string的原始字串值

console.log("test".valueof());//"test"

console.log("test".tostring());//"test"

console.log("test".tolocalestring());//"test"

字串的訪問字元方法總共有chartat()、中括號、charcodeat()和fromcharcode()四種

【chartat()】

charat()方法接收乙個基於0的字元位置的引數,返回指定位置的字元。當引數為空或nan時,預設引數為0;當引數超出範圍時,則返回乙個空字串 

var str = "hello";

console.log(str.charat(1));//e

console.log(str.charat(-1));//''

console.log(str.charat(10));//''

console.log(str.charat());//h

console.log(str.charat(nan));//h

charat()方法涉及到number()函式的隱式型別轉換,如果轉換為數值,則按照上述規則輸出字串;如果轉換為nan,則輸出第0個字元

var str = "hello";

console.log(str.charat(true));//'e'

console.log(str.charat(false));//'h'

console.log(str.charat('abc'));//'h'

console.log(str.charat({}));//'h'

console.log(str.charat([2]));//'l'

[注意]x.charat(pos)與x.substring(pos, pos+1)、x.substr(pos,1)、x.slice(pos,pos+1)的結果相等

var str = "hello";

console.log(str.charat(1));//'e'

console.log(str.substring(1,2));//'e'

console.log(str.slice(1,2));//'e'

console.log(str.substr(1,1));//'e'

【中括號】

ecmascript5定義了另乙個訪問字元的方法,使用方括號加數字索引來訪問字串中的特定字元。如果引數超出範圍或是nan時,則輸出undefined;沒有引數時,會報錯;該方法沒有number()轉型函式的隱式型別轉換,但引數為單數值陣列時可轉換為數值

[注意]ie7-瀏覽器不支援

var str = "hello";

console.log(str[0]);//h

console.log(str[[1]]);//e

console.log(str[false]);//undefined

console.log(str[-1]);//undefined

console.log(str[nan]);//undefined

console.log(str);//報錯

【charcodeat()】

charcodeat()方法類似於charat()方法,接收乙個基於0的字元位置的引數,但返回的是指定位置的字元16位unicode編碼。返回值是乙個16位的整數,在0-65535之間,即0x0000-0xffff之間

引數為空或nan時,預設引數為0;當引數超出範圍時,則返回nan

var str = "hello";

console.log(str.charcodeat());//104

console.log(str.charcodeat(0));//104

console.log(str.charcodeat(1));//101

console.log(str.charcodeat(-1));//nan

console.log(str.charcodeat(10));//nan

console.log(str.charcodeat(nan));//104

同樣地,charcodeat()方法涉及到number()函式的隱式型別轉換,如果轉換為數值,則按照上述規則輸出相應值;如果轉換為nan,則輸出第0個字元的字元編碼

var str = "hello";

console.log(str.charcodeat(true));//101

console.log(str.charcodeat(false));//104

console.log(str.charcodeat('abc'));//104

console.log(str.charcodeat({}));//104

console.log(str.charcodeat([2]));//l08

【fromcharcode()】

string建構函式本身有乙個靜態方法:fromcharcode()。這個方法的任務是接收乙個或多個字元編碼,然後把它們轉換成乙個字串。從本質上看,這個方法與例項方法charcodeat()執行的是相反的操作。若引數為空為nan時,則返回空字串;若引數超出0-65535的範圍,則輸出字元不可控

console.log(string.fromcharcode(104,101,108,108,111));//'hello'

console.log(string.fromcharcode(0x6211,0x662f,0x5c0f,0x706b,0x67f4));//'我是小火柴'

console.log(string.fromcharcode());//''

console.log(string.fromcharcode(nan));//''

console.log(string.fromcharcode(-1));

console.log(string.fromcharcode(65560));

如果乙個字元占用四位元組,則需要拆成兩個字元表示

console.log(string.fromcharcode(0xd842, 0xdfb7)); // "

string類的常用屬性和方法

靜態方法 string.format 格式化字串 string.concat string,string 字串連線 string.compare string,string 字串比較。相等返回0,大於返回1,小於返回 1 string.comparenocase 字串比較不區分大小寫 例項方法 st...

swift 的型別屬性和方法

型別屬性 使用關鍵字static來定義結構體和列舉的型別屬性,關鍵字class來為類定義型別屬性。語法 struct structname enum enumname class classname 類似於例項的屬性,型別屬性的訪問也是通過點運算子 來進行。但是,型別屬性是通過型別本身來獲取和設定,...

js中String物件的常用方法和屬性

string物件的方法 charat charcodeat indexof match replace search slice touppercase tolowercase 等方法 length 字串的長度 charat 索引 返回值是指定索引位置的字串,超出索引返回空字串 concat 字串1...