定义

  1. var str = '';
  2. var str = new String('');
  3. var str = String('');
  4. typeof('hello') //"string"

计算字符串长度

  1. str.length
  2. // null undefine

读取指定

  1. 'error'['2']
  2. "r"
  3. 'error'[2]
  4. "r"
  5. 'error'.2
  6. VM316:1 Uncaught SyntaxError: Unexpected number

字符索引号—匹配

  1. str.indexOf(searchValue[, fromIndex])
  2. str.lastIndexOf(searchValue[, fromIndex])
  3. //ex
  4. var str = '2-3-21-2'
  5. str.indexOf('2') //0
  6. str.lastIndexOf('2') //7
  7. str.search(regexp)
  8. ???????????
  9. str.endsWith(searchString[, length])
  10. 'Hello word!'.endsWith('!') //true
  11. 'Hello word!'.endsWith('wo', 8) //true
  12. 'Hello word!'.endsWith('wo', 7) //false
  13. str.includes(searchString[, position])
  14. 'Hello word!'.includes('hello') //false
  15. 'Hello word!'.includes('ello', 1) //true
  16. 'Hello word!'.includes('ello', 4) //false
  17. str.match(regexp)
  18. ????????????

截取

  1. str.slice(beginIndex[, endIndex])
  2. 'hello'.slice(1) //"ello"
  3. 'hello'.slice(1,4) //"ell"
  4. str.substr(start[, length])
  5. 'Hello'.substr(1) //"ello"
  6. 'Hello'.substr(1,2) //"el"
  7. str.substring(indexStart[, indexEnd])(不推荐)
  8. 'Hello'.substring(1) //"ello"
  9. 'Hello'.substring(1,4) //"ell"
  10. //slice、substring区别:slice中的index可以为负数

拼接

  1. str.concat(string2[, string3, ..., stringN])
  2. 'Hello'.concat(' Word!') //"Hello Word!"
  3. str.padEnd(targetLength [, padString])
  4. 'hello'.padEnd(6); //"hello "
  5. 'hello'.padEnd(14, ' word!'); //"hello word! wo"
  6. str.padStart(targetLength [, padString])
  7. 'word'.padStart(6); //" word"
  8. 'word'.padStart(12, 'Hello '); //"Hello Heword"
  9. str.repeat(count);
  10. 'Hello'.repeat(1.5) //"Hello"
  11. 'Hello'.repeat(2) //"HelloHello"

格式化

  1. 'Hello'.toUpperCase() //"HELLO"
  2. 'Hello'.toLowerCase() //"hello"
  3. ' Hello '.trim() //"Hello"
  4. ' Hello '.trimLeft() //"Hello "
  5. ' Hello '.trimRight() //" Hello"
  6. str.replace(regexp|substr, newSubstr|function)
  7. ??????????????

填充函数

  1. 'bcd'.padStart(4)// " bcd"
  2. 'bcd'.padEnd(4)// "bcd "
  3. 'bcd'.padLeft(4)// " bcd"
  4. 'bcd'.padRight(4)// "bcd "

场景

  1. '0.0'.padStart(10) // " 0.0"
  2. '188,000.0'.padStart(10)// " 188,000.0"
  3. '0.0'.padStart(10, '-') // "-------0.0"

charAt

  1. /*==============string=================*/
  2. var str1 = "abcdefgc";
  3. console.log(str1.length); //8
  4. /*charAt() 固定位置的字符 默认是首字母 0开始计数*/
  5. console.log(str1.charAt()); //a
  6. console.log(str1.charAt(0)); //a
  7. console.log(str1.charAt(2)); //c
  8. /*str[num] 支持IE7以上版本*/
  9. //console.log(str1[]); //error
  10. console.log(str1[0]); //a
  11. console.log(str1[2]); //c
  12. /*charCodeAt() 固定位置的字符编码 */
  13. console.log(str1.charCodeAt()); //97
  14. console.log(str1.charCodeAt(0)); //97
  15. console.log(str1.charCodeAt(2)); //99

sting == arr

  1. //转【】
  2. str.split([separator[, limit]])
  3. var str = '2-3-21-2'
  4. str.split("-"); //["2", "3", "21", "2"]
  5. str.indexOf('2') //0
  6. str.lastIndexOf('2') //7
  7. //转str
  8. [2,3].join(",") // "2,3"
  9. Object.values('err')
  10. // (3) ["e", "r", "r"]
  11. Object.keys('err')
  12. // (3) ["0", "1", "2"]

返回index的utf-16编码

  1. js内部utf-16存储
  2. character = str.charAt(index);
  3. 'hello'.charAt(1); \\ "e"
  4. '\u0068\u0065\u006c\u006c\u006f'.charAt(1); \\"e"

转标签

  1. 'hello'.sub() //"<sub>hello</sub>"
  2. 'hello'.sup() //"<sup>hello</sup>"

replace/replaceAll替换

返回一个替换以后的新字符串。

  1. // 替换第一个
  2. '23-21312-dwe-342-3dsd'.replace('-', '')
  3. "2321312-dwe-342-3dsd"
  4. // replaceAll, 替换全部
  5. '23-21312-dwe-342-3dsd'.replace(/-/g, '')
  6. "2321312dwe3423dsd"

插入

  1. function insert(oldStr, insertPos, insertStr ) {
  2. oldStr = oldStr.slice(0, insertPos) + insertStr + oldStr.slice(insertPos)
  3. return oldStr;
  4. }
  5. // test
  6. var b = '112233'
  7. insert(b, 2, '88')
  8. "11882233"