JavaScript里处理字符串的一些经常使用方法

1.length 属性返回字符串的长度html

let srt = "hello world!"; console.log(srt.length) // 12

2.indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。es6

let str="Hello world!" console.log(str.indexOf("Hello")); // 0
console.log(str.indexOf("World")); // -1
console.log(str.indexOf("world")); // 6
  • indexOf() 方法对大小写敏感!

若是要检索的字符串值没有出现,则该方法返回 -1。经常使用来判断是否含有该字符正则表达式

let url = 'https://www.cnblogs.com/imMeya/p/11492490.html'
      if(url.indexOf("?") == -1){ console.log("url中没有'?'"); }else{ console.log("含有"); } // -> url中没有'?'

3.lastIndexOf() 方法返回指定文本在字符串中最后一次出现的索引数组

  • 若是未找到文本, indexOf() 和 lastIndexOf() 均返回 -1
  •  indexOf() 和 lastIndexOf()两种方法都接受做为检索起始位置的第二个参数。
let srt = "hello world! hello china"; console.log(srt.lastIndexOf('hello')) // 13
console.log(srt.indexOf('hello',5)) //13

4.search() 方法搜索特定值的字符串,并返回匹配的位置less

  • indexOf() 与 search(),是相等的。

这两种方法的区别在于:编码

  1. search() 方法没法设置第二个开始位置参数。
  2. indexOf() 方法没法设置更强大的搜索值(正则表达式)。
let str="Hello world!" console.log(str.search("Hello")); // 0
console.log(str.search("World")); // -1
console.log(str.search("world")); // 6

 

5.substring() 方法用于提取字符串中介于两个指定下标之间的字符。url

  • substring() 不接受负的参数。
let str="Hello world!" console.log(str.substring(3,7)); // lo wo
console.log(str.substring(3)); // lo world!

6.slice() 方法可提取字符串的某个部分,并以新的字符串返回被提取的部分。spa

let str="Hello world!" console.log(str.slice(3,7)); // lo w
console.log(str.slice(3)); // lo world!
console.log(str.slice(-2)); // d! 。若是是负数,则该参数规定的是从字符串的尾部开始算起的位置。也就是说,-1 指字符串的最后一个字符,-2 指倒数第二个字符,以此类推。

7.substr() 方法可在字符串中抽取从 start 下标开始的指定数目的字符。prototype

stringObject.substr(start,length)
let str="Hello world!" console.log(str.substr(3)); // lo world!
console.log(str.substr(3,2)); // lo

 

String 对象的方法 slice()、substring()均可返回字符串的指定部分。slice() 比 substring() 要灵活一些,由于它容许使用负数做为参数。code

8.replace() 方法用另外一个值替换在字符串中指定的值。

  • replace() 方法不会改变调用它的字符串。它返回的是新字符串。
  • replace() 方法对大小写敏感!
let str = "Hello world!"; console.log(str.replace('Hello','Hi'));  // Hi world! 
  •  如需执行大小写不敏感的替换,请使用正则表达式 /i(忽略大小写)
  • 请注意正则表达式不带引号。
let str = "Hello world!"; console.log(str.replace(/hello/i,'Hi'));  // Hi world! 

默认地,replace() 只替换首个匹配

如需替换全部匹配,请使用正则表达式的 g 标志(用于全局搜索)

let str = "Hello world! hello China !"; console.log(str.replace(/hello/ig,'Hi'));  // Hi world! Hi China ! 

9.toUpperCase() 把字符串转换为大写

let text = 'Hello world!'; console.log(text.toUpperCase()) //HELLO WORLD!

10.toLowerCase() 把字符串转换为小写

let text = 'HELLO WORLD!';
console.log(text.toLowerCase()) // hello world!

11.concat() 链接两个或多个字符串

  • concat() 方法可用于代替加运算符。
  • 全部字符串方法都会返回新字符串。它们不会修改原始字符串
let text1 = "Hello"; let text2 = "World!"; let text3 = text1.concat(" ",text2); console.log(text3); //Hello world!
  • concat() 方法可用于代替加运算符。下面两行是等效的
let text = "Hello" + " " + "World!"; let text = "Hello".concat(" ","World!"); //Hello world!

12.trim() 方法删除字符串两端的空白符

let str = "       Hello World!        "; console.log(str.trim()); //Hello world!
  • Internet Explorer 8 或更低版本不支持 trim() 方法。
  • 如需支持 IE 8,您可搭配正则表达式使用 replace() 方法代替
let str = "       Hello World!        "; console.log(str.trim()); //Hello world!
console.log(str.replace(/\s+/g,'')); // HelloWorld! 去掉全部空格
console.log(str.replace(/(^\s*)|(\s*$)/g,'')); //Hello world! 去掉先后空格

13.charAt() 方法返回字符串中指定下标(位置)的字符串

let str = 'Hello World!'; console.log(str.charAt(0)) //H
console.log(str[0]); //H 此方法不建议使用

14.charCodeAt() 方法返回字符串中指定索引的字符 unicode 编码

let str = 'Hello World!'; console.log(str.charCodeAt(0)) // 72

15.split() 将字符串转换为数组

let txt = "h,e,l,l,o"; console.log(txt.split(","));  // ["h", "e", "l", "l", "o"] 用逗号分隔
console.log(txt.split(" "));  // ["h,e,l,l,o"] 用空格分隔 //若是分隔符是 "",被返回的数组将是间隔单个字符的数组
let txt2 = "Hello"; console.log(txt2.split(""));   // ["h", "e", "l", "l", "o"] 分隔为字符

16.match()使用正则表达式与字符串相比较。

var str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; var regexp = /[A-E]/gi; var matches_array = str.match(regexp); console.log(matches_array); // ['A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']

使用match不传参数

var str = "Nothing will come of nothing."; str.match(); // [""]

 

17.padEnd()在当前字符串尾部填充指定的字符串, 直到达到指定的长度。 返回一个新的字符串。

  • srt.padEnd(targetLength [,padString])
  • 当前字符串须要填充到的目标长度。若是这个数值小于当前字符串的长度,则返回当前字符串自己。
'abc'.padEnd(10);          // "abc "
'abc'.padEnd(10, "foo");   // "abcfoofoof"
'abc'.padEnd(6, "123456"); // "abc123"
'abc'.padEnd(1);           // "abc"

 

18.padStart()在当前字符串头部填充指定的字符串, 直到达到指定的长度。 返回一个新的字符串。

  • srt.padEnd(targetLength [,padString])
  • 当前字符串须要填充到的目标长度。若是这个数值小于当前字符串的长度,则返回当前字符串自己。
'abc'.padStart(10);         // " abc"
'abc'.padStart(10, "foo");  // "foofoofabc"
'abc'.padStart(6,"123465"); // "123abc"
'abc'.padStart(8, "0");     // "00000abc"
'abc'.padStart(1);          // "abc"

 

19.repeat()返回指定重复次数的由元素组成的字符串对象。

"abc".repeat(-1)     // RangeError: repeat count must be positive and less than inifinity
"abc".repeat(0)      // ""
"abc".repeat(1)      // "abc"
"abc".repeat(2)      // "abcabc"
"abc".repeat(3.5)    // "abcabcabc" 参数count将会被自动转换成整数.
"abc".repeat(1/0)    // RangeError: repeat count must be positive and less than inifinity ({toString : () => "abc", repeat : String.prototype.repeat}).repeat(2) //"abcabc",repeat是一个通用方法,也就是它的调用者能够不是一个字符串对象.

es6新增 

20.includes()直接用来判断字符串是否存在

let str = 'Hello world"; console.log(str.includes('abc') // false

21.startsWith()判断该字符串是否以searchString开头(能够用来检测是不是地址)

let str = 'http://www.baidu.com'; console.log(str.startsWith('http://')); // true

22.endsWith()判断该字符串是否以searchString结尾(能够用来检测文件扩展名)。

let str = './images/icon/login.png'; console.log(str.endsWith('.png')); // true

 

原文出处:https://www.cnblogs.com/imMeya/p/11498944.html

相关文章
相关标签/搜索