創(chuàng)建字符串的方法
- 字面量方式
let str = '123'
- 類方法創(chuàng)建
let str = new String('123')
console.log(str) // String {"123"}
// 當(dāng)然可以通過(guò)valueOf或者toString方法把其轉(zhuǎn)化成和字面量創(chuàng)建的一樣
str.valueOf() // 或者str.toString()
console.log(str) // '123'
String類私有方法
-
slice方法
/* 方法:slice(start,end) * 功能:拷貝字符串中的某段數(shù)據(jù)言蛇,對(duì)原字符串不會(huì)產(chǎn)生改變 * 返回值:返回拷貝了某段數(shù)據(jù)的新字符串 * 參數(shù):start:起始位沥邻,從0開(kāi)始 * end(可選):結(jié)束位 */ let string = 'happy day' let stringSlice= string.slice(1,2) // 從位置1即第二個(gè)字符開(kāi)始轮傍,拷貝到位置為2前 console.log(string ,stringSlice) //string:'happy day' stringSlice:'a' stringSlice= string .slice(0)// 拷貝整個(gè)字符串 console.log(string ,stringSlice) //string:'happy day' stringSlice:'happy day'
-
split方法
/* 方法:split(separator, limit) * 功能:以指定的分割符對(duì)字符串進(jìn)行分割 * 返回值:返回以分隔符分割成的數(shù)組 * 參數(shù):separator:分隔符 * limit(可選):限制分割的份數(shù) */ let string = 'happy day and day' let ArraySplit= string.split(' ') // 以空格進(jìn)行分割 console.log(string ,ArraySplit) //string:'happy day and day' ArraySplit:['happy','day','and','day'] ArraySplit= string.split(' ',2) // 以空格進(jìn)行分割地梨,并提取2份 console.log(string ,ArraySplit) //string:'happy day and day' ArraySplit:['happy','day']
-
substring方法
/* 方法:substring(start, end) * 功能:截取指定位置的字符串 * 返回值:返回截取到的字符串 * 參數(shù):start:起始位置乖订,從0開(kāi)始 * end(可選):結(jié)束位置 */ let string = 'happy day' let stringSubstring= string.substring(0,4) // 從開(kāi)頭位置截取到位置4之前 console.log(string ,stringSubstring) //string:'happy day' stringSubstring:'happ' stringSubstring= string.substring(5) // 從位置5開(kāi)始截取到完 console.log(string ,stringSubstring) //string:'happy day' stringSubstring:'day'
-
charAt方法
/* 方法:charAt(index) * 功能:從字符串中返回指定的字符 * 返回值:返回的字符 * 參數(shù):index:指定位置痴突,從0開(kāi)始 */ let string = 'happy day' let char= string.charAt(4) // 讀取第五個(gè)字符 console.log(string ,char) //string:'happy day' char:'y'
-
charCodeAt方法
/* 方法:charCodeAt(index) * 功能:從字符串中返回指定字符的16位Unicode編碼 * 返回值:Unicode編碼 * 參數(shù):index:指定位置辫塌,從0開(kāi)始 */ let string = 'happy day' let charcode= string.charCodeAt(4) // 讀取第五個(gè)字符 console.log(string ,charcode) //string:'happy day' charcode:121
-
concat方法
/* 方法:concat(str1,str2,str3...) * 功能:字符串拼接 * 返回值:返回拼接完的新數(shù)組 * 參數(shù): * str1:拼接的字符串 */ let str1 = 'happy day' let str2 = 'and day' let str3 = str1.concat(str2) // 讀取第五個(gè)字符 console.log(str1,str2,str3 ) //str1:'happy day' str2:'and day' str3:'happy dayand day' // 更建議使用[賦值操作符](`+`, `+=`)
-
codePointAt方法
/* 方法:codePointAt(index) * 功能:從字符串中返回指定字符的Unicode編碼 * 返回值:Unicode編碼 * 參數(shù):index:指定位置原押,從0開(kāi)始 */ let string = 'happy day' let charcode= string.codePointAt(4) // 讀取第五個(gè)字符 console.log(string ,charcode) //string:'happy day' charcode:121 // 注意其與charCodeAt方法的區(qū)別:charCodeAt是用2個(gè)字節(jié)編碼(碼元),codePointAt使用4個(gè)字節(jié)編碼(碼點(diǎn))
-
endsWith方法
/* 方法:endsWith(str圾另,index) * 功能:判斷給定字符或者是字符串是否是該字符串的結(jié)尾 * 返回值:true或false * 參數(shù):str:字符串 * index(可選):把第幾位當(dāng)作是末尾(以1開(kāi)始) */ let string = 'happy day' let endwith= string.endsWith('ay') // 末尾是以’ay‘結(jié)尾 console.log(string ,endwith) //string:'happy day' charcode:true endwith= string.endsWith('app',4) // 把第四位當(dāng)作是末尾霸株,判斷’app‘是不是結(jié)尾 console.log(string ,endwith) //string:'happy day' charcode:true
-
includes方法
/* 方法:includes(searchStr,index) * 功能:查找字符串是否包含提供的字符串 * 返回值:true或false * 參數(shù):searchStr:查找的字符串 * index(可選):從第幾位找起(從0開(kāi)始) */ let string = 'happy day' let includeFlag= string.includes('ay') // 查找是否存在'ay' console.log(string ,includeFlag) //string:'happy day' includeFlag:true includeFlag= string.includes('day',5) // 從第六個(gè)字符開(kāi)始查找是否存在’day' console.log(string ,includeFlag) //string:'happy day' includeFlag: true
-
indexOf方法
/* 方法:indexOf(searchStr集乔,index) * 功能:查找某字符串在該字符串中第一次出現(xiàn)的索引位置 * 返回值:索引位置 * 參數(shù):searchStr:查找的字符串 * index(可選):從第幾位找起(從0開(kāi)始) */ let string = 'happy day' let indexof= string.indexOf('ppy') // 查找’ppy'在字符串中第一次出現(xiàn)的索引位置 console.log(string ,indexof) //string:'happy day' indexof: 2 indexof= string.indexOf('ppy',2) // 從索引位置為2開(kāi)始查找’ppy‘的索引位置 console.log(string ,indexof) //string:'happy day' indexof: 2
-
toUpperCase方法
/* 方法:toUpperCase() * 功能:把字符串轉(zhuǎn)化為大寫 * 返回值:轉(zhuǎn)換為大寫的新字符串 */ let string = 'happy day' let upperCase= string.toUpperCase() // 把字符串轉(zhuǎn)換成大寫 console.log(string ,upperCase) //string:'happy day' upperCase: 'HAPPY DAY'
-
toLowerCase方法
/* 方法:toLowerCase() * 功能:把字符串轉(zhuǎn)化為小寫 * 返回值:轉(zhuǎn)換為小寫的新字符串 */ let string = 'HAPPY DAY' let upperCase= string.toLowerCase() // 把字符串轉(zhuǎn)換成小寫 console.log(string ,upperCase) //string:'HAPPY DAY' upperCase: 'happy day'
-
trim方法
/* 方法:trim() * 功能:去掉字符串兩端的空白字符 (space, tab, no-break space 等) 以及所有行終止符字符(如 LF去件,CR等) * 返回值:返回去掉空白字符的字符串 */ let string = ' happy day ' let trimstr= string.trim() // 去掉字符串兩端的空白字符 console.log('string:' ,string ,'trimstr:',trimstr) //string:' happy day ' trimstr: 'happy day'