码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-09-11 13:57:19      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:连接   替换   sub   没有   字符   console   har   一个   转换   

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

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

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

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 

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

这两种方法是不相等的。区别在于:

  1. search() 方法无法设置第二个开始位置参数。
  2. indexOf() 方法无法设置更强大的搜索值(正则表达式)。

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

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

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

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

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

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() 要灵活一些,因为它允许使用负数作为参数。

主要:slice()、substring()是截取两个下标直接的部分,substr是截图下标开始的位置!

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"]  分隔为字符

 

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

标签:连接   替换   sub   没有   字符   console   har   一个   转换   

原文地址:https://www.cnblogs.com/imMeya/p/11498944.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!