js字符串常用方法

1. str.charAt(index);         返回子字符串 ,(index是字符下标

  	const str = 'hello world';
  	console.log(str.charAt(0));   //h

 

2.  str.charCodeAt(index);   返回子字符串的unicode编码

  	const str = 'hello world';
  	console.log(str.charCodeAt(0));   //104

 

3. String.fromCharCode(num1,num2,...,numN); 根据unicode编码返回字符串

console.log(String.fromCharCode(104));   //h

 

4. str.indexOf(searchString,startIndex);  返回子字符串第一次出现的位置,从startIndex开始查找,找不到时返回-1

  	const str = 'hello world';
  	console.log(str.indexOf('r', 8));   //8
  	console.log(str.indexOf('r', 9));   //-1

 

5. str.lastIndexOf(searchString,startIndex);  从后往前找子字符串,找不到时返回-1

  	const str = 'hello world';
  	console.log(str.lastIndexOf('r', 7));   //-1
  	console.log(str.lastIndexOf('r', 9));   //8

 

6.字符串之间的比较:比较第一个字符的unicode编码值,第一个字符要是相同,就比较第二个,依次往下

  	console.log('10000' > '2');   //false  1的unicode值比2的unicode值小 
  	console.log('10000' > 2);    //true   转成数字比较

 

7.  截取字符串:

  1. str.substring(start,end);      //参数为负数的话自动转为0
  2. str.slice(start,end);         //参数为负数则从后面开始选取
  3. str.substr(start,length);(不建议使用,因为ECMAscript 没有对该方法进行标准化,反对使用它。)  //start参数为负数从后面开始选取,length必须为正数
  	const str = 'hello world';
  	console.log(str.substring(-2, 7));   //hello w
  	console.log(str.substr(-2, 1));   //l
  	console.log(str.slice(-2, -1));   //l

 

8. 字符串分割成数组。 str.split(separator,limit);  参数1指定字符串或正则,参照2指定数组的最大长度

  	const str = 'hello world';
  	console.log(str.split());   //["hello world"]
  	console.log(str.split('', 2));   //["h", "e"]
  	console.log(str.split(' '));   //["hello", "world"]

 

9. str.match(rgExp);  正则匹配,匹配到的字符串返回数组。

  	const str = 'hello world';
  	console.log(str.match(/l/g));   //["l", "l", "l"]

 

10. str.replace(rgExp/substr,replaceText)   返回替换后的字符串

  	const str = 'hello world';
  	console.log(str.replace(/l/g, 'A'));   //heAAo worAd

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaScript中字符串有很多常用方法,下面是一些常见的方法: 1. `length`:返回字符串的长度。 ```javascript const str = "Hello World"; console.log(str.length); // 输出 11 ``` 2. `charAt(index)`:返回指定索引位置的字符。 ```javascript const str = "Hello"; console.log(str.charAt(0)); // 输出 "H" console.log(str.charAt(3)); // 输出 "l" ``` 3. `substring(start, end)`:返回从start到end(不包括end)之间的子字符串。 ```javascript const str = "Hello World"; console.log(str.substring(0, 5)); // 输出 "Hello" console.log(str.substring(6)); // 输出 "World" ``` 4. `slice(start, end)`:返回从start到end(不包括end)之间的子字符串,与substring类似。 ```javascript const str = "Hello World"; console.log(str.slice(0, 5)); // 输出 "Hello" console.log(str.slice(6)); // 输出 "World" ``` 5. `indexOf(substring)`:返回substring在字符串中第一次出现的索引位置,如果没有找到则返回-1。 ```javascript const str = "Hello World"; console.log(str.indexOf("o")); // 输出 4 console.log(str.indexOf("z")); // 输出 -1 ``` 6. `lastIndexOf(substring)`:返回substring在字符串中最后一次出现的索引位置,如果没有找到则返回-1。 ```javascript const str = "Hello World"; console.log(str.lastIndexOf("o")); // 输出 7 console.log(str.lastIndexOf("z")); // 输出 -1 ``` 7. `toLowerCase()`:将字符串转换为小写。 ```javascript const str = "Hello World"; console.log(str.toLowerCase()); // 输出 "hello world" ``` 8. `toUpperCase()`:将字符串转换为大写。 ```javascript const str = "Hello World"; console.log(str.toUpperCase()); // 输出 "HELLO WORLD" ``` 这只是一些常见的字符串方法,JavaScript中还有很多其他有用的方法

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值