JavaScript 字符串方法运用

1、indexOf() 方法,返回字符串中指定文本首次出现的索引(位置),如果不存在该文本返回“-1”,第二个参数为检索开始的位置,直到结束位置。

var str = "The full name of China is the People's Republic of China.";
var pos = str.indexOf("China");//返回结果 17

2、lastIndexOf() 方法,返回指定文本在字符串中最后一次出现的索引(位置),如果不存在该文本返回“-1”,第二个参数为检索开始的位置,直到开始位置。

var str = "The full name of China is the People's Republic of China.";
var pos = str.lastIndexOf("China");//返回结果 51

3、search() 方法,返回字符串中指定文本首次出现的索引(位置),如果不存在该文本返回“-1”,支持正则表达式的查询。

var str = "The full name of China is the People's Republic of China.";
var pos = str.search("China");//返回结果 17

4、slice()方法,字符串的截取,第一个参数:起始索引(开始位置),第二个参数:终止索引(结束位置),包含第一个参数,不包含第二个参数,支持负数,如果为负数检索是从后往前。

var str = "The full name of China is the People's Republic of China.";
var pos = str.slice(1,5);//返回结果 he f

5、substring()方法,字符串的截取,第一个参数:起始索引(开始位置),第二个参数:终止索引(结束位置),包含第一个参数,不包含第二个参数。

var str = "The full name of China is the People's Republic of China.";
var pos = str.substring(1,5);//返回结果 he f

6、substr()方法,字符串的截取,第一个参数:起始索引(开始位置),第二个参数:截取的长度,如果没有该参数,截取到末尾。

var str = "The full name of China is the People's Republic of China.";
var pos = str.substr(1,5);//返回结果 he fu

7、replace()方法,用另一个值替换在字符串中指定的值,只替换第一个匹配值,不会改变调用它的字符串。它返回的是新字符串。

var str = "The full name of China is the People's Republic of China.";
var pos = str.replace('China','wegame');//返回结果 The full name of wegame is the People's Republic of China.

8、toUpperCase()方法,把字符串转换为大写。

var str = "China";
var pos = str.toUpperCase();//返回结果 CHINA

9、toLowerCase() 方法,把字符串转换为小写。

var str = "China";
var pos = str.toUpperCase();//返回结果 china

10、concat()方法,连接两个或多个字符串。

var str1= "Hello";
var str2= "World";
str3= str1.concat(" ",str2);//返回结果"Hello World"

11、trim() 方法,删除字符串两端的空白符。

var str = "       Hello World!        ";
var pos=str.trim();//返回结果"Hello World!"

12、charAt() 方法,返回字符串中指定下标(位置)的字符串

var str = "HELLO WORLD";
str.charAt(0);            // 返回结果 H

13、charCodeAt() 方法,返回字符串中指定索引的字符 unicode 编码:

var str = "HELLO WORLD";
str.charCodeAt(0);         // 返回 72
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值