定义
var str = '';
var str = new String('');
var str = String('');
typeof('hello') //"string"
计算字符串长度
str.length
// null undefine
读取指定
'error'['2']
"r"
'error'[2]
"r"
'error'.2
VM316:1 Uncaught SyntaxError: Unexpected number
字符索引号—匹配
str.indexOf(searchValue[, fromIndex])
str.lastIndexOf(searchValue[, fromIndex])
//ex
var str = '2-3-21-2'
str.indexOf('2') //0
str.lastIndexOf('2') //7
str.search(regexp)
???????????
str.endsWith(searchString[, length])
'Hello word!'.endsWith('!') //true
'Hello word!'.endsWith('wo', 8) //true
'Hello word!'.endsWith('wo', 7) //false
str.includes(searchString[, position])
'Hello word!'.includes('hello') //false
'Hello word!'.includes('ello', 1) //true
'Hello word!'.includes('ello', 4) //false
str.match(regexp)
????????????
截取
str.slice(beginIndex[, endIndex])
'hello'.slice(1) //"ello"
'hello'.slice(1,4) //"ell"
str.substr(start[, length])
'Hello'.substr(1) //"ello"
'Hello'.substr(1,2) //"el"
str.substring(indexStart[, indexEnd])(不推荐)
'Hello'.substring(1) //"ello"
'Hello'.substring(1,4) //"ell"
//slice、substring区别:slice中的index可以为负数
拼接
str.concat(string2[, string3, ..., stringN])
'Hello'.concat(' Word!') //"Hello Word!"
str.padEnd(targetLength [, padString])
'hello'.padEnd(6); //"hello "
'hello'.padEnd(14, ' word!'); //"hello word! wo"
str.padStart(targetLength [, padString])
'word'.padStart(6); //" word"
'word'.padStart(12, 'Hello '); //"Hello Heword"
str.repeat(count);
'Hello'.repeat(1.5) //"Hello"
'Hello'.repeat(2) //"HelloHello"
格式化
'Hello'.toUpperCase() //"HELLO"
'Hello'.toLowerCase() //"hello"
' Hello '.trim() //"Hello"
' Hello '.trimLeft() //"Hello "
' Hello '.trimRight() //" Hello"
str.replace(regexp|substr, newSubstr|function)
??????????????
填充函数
'bcd'.padStart(4)// " bcd"
'bcd'.padEnd(4)// "bcd "
'bcd'.padLeft(4)// " bcd"
'bcd'.padRight(4)// "bcd "
场景
'0.0'.padStart(10) // " 0.0"
'188,000.0'.padStart(10)// " 188,000.0"
'0.0'.padStart(10, '-') // "-------0.0"
charAt
/*==============string=================*/
var str1 = "abcdefgc";
console.log(str1.length); //8
/*charAt() 固定位置的字符 默认是首字母 0开始计数*/
console.log(str1.charAt()); //a
console.log(str1.charAt(0)); //a
console.log(str1.charAt(2)); //c
/*str[num] 支持IE7以上版本*/
//console.log(str1[]); //error
console.log(str1[0]); //a
console.log(str1[2]); //c
/*charCodeAt() 固定位置的字符编码 */
console.log(str1.charCodeAt()); //97
console.log(str1.charCodeAt(0)); //97
console.log(str1.charCodeAt(2)); //99
sting == arr
//转【】
str.split([separator[, limit]])
var str = '2-3-21-2'
str.split("-"); //["2", "3", "21", "2"]
str.indexOf('2') //0
str.lastIndexOf('2') //7
//转str
[2,3].join(",") // "2,3"
Object.values('err')
// (3) ["e", "r", "r"]
Object.keys('err')
// (3) ["0", "1", "2"]
返回index的utf-16编码
js内部utf-16存储
character = str.charAt(index);
'hello'.charAt(1); \\ "e"
'\u0068\u0065\u006c\u006c\u006f'.charAt(1); \\"e"
转标签
'hello'.sub() //"<sub>hello</sub>"
'hello'.sup() //"<sup>hello</sup>"
replace/replaceAll替换
返回一个替换以后的新字符串。
// 替换第一个
'23-21312-dwe-342-3dsd'.replace('-', '')
"2321312-dwe-342-3dsd"
// replaceAll, 替换全部
'23-21312-dwe-342-3dsd'.replace(/-/g, '')
"2321312dwe3423dsd"
插入
function insert(oldStr, insertPos, insertStr ) {
oldStr = oldStr.slice(0, insertPos) + insertStr + oldStr.slice(insertPos)
return oldStr;
}
// test
var b = '112233'
insert(b, 2, '88')
"11882233"