JS字符串下的方法

  • length:返回字符串的长度
var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var sln = txt.length;
console.log(sln); //26
  • indexOf() :返回字符串中指定文本首次出现的索引(位置)zhu
var str = "The full name of China is the People's Republic of China.";
var pos = str.indexOf("China"); //17

 注:

JavaScript 从零计算位置。

0 是字符串中的第一个位置,1 是第二个,2 是第三个 ...

  •  lastIndexOf() :返回指定文本在字符串中最后一次出现的索引
var str = "The full name of China is the People's Republic of China."; 
var pos = str.lastIndexOf("China"); //51

注:如果未找到文本, indexOf() 和 lastIndexOf() 均返回 -1。

  •  indexOf()和lastIndexOf()都接受作为检索起始位置的第二个参数
var str = "The full name of China is the People's Republic of China.";
var pos = str.indexOf("China", 18); //51

var str = "The full name of China is the People's Republic of China.";
var pos = str.lastIndexOf("China", 50); //17
  •  search() :搜索特定值的字符串,并返回匹配的位置
var str = "The full name of China is the People's Republic of China.";
var pos = str.search("locate"); //-1

indexOf() 与 search(),区别在于:

  • search() :无法设置第二个开始位置参数。
  • indexOf() :无法设置更强大的搜索值(正则表达式)。
  • slice() 提取字符串的某个部分并在新字符串中返回被提取的部分。

该方法设置两个参数:起始索引(开始位置),终止索引(结束位置)。

var str = "Apple, Banana, Mango";
var res = str.slice(7, 13);  //Banana

 如果某个参数为负,则从字符串的结尾开始计数。

var str = "Apple, Banana, Mango";
var res = str.slice(-13,-7);  //Banana

 如果省略第二个参数,则该方法将裁剪字符串的剩余部分

var str = "Apple, Banana, Mango";
var res = str.slice(7);  //Banana, Mango
  •  substring() 类似于 slice(),不同的是substring() 无法接受负的索引
var str = "Apple, Banana, Mango";
var res = str.substring(7, 13);  //Banana
  • substr()类似于 slice(),不同之处在于第二个参数规定被提取部分的长度

var str = "Apple, Banana, Mango";
var res = str.substr(7, 6);  //Banana

 如果首个参数为负,则从字符串的结尾计算位置

var str = "Apple, Banana, Mango";
var res = str.substr(-5);  //Mango
  •  replace():用另一个值替换在字符串中指定的值,默认值替换首个匹配,且对大小写敏感
var str = "Please visit Microsoft!";
var n = str.replace("Microsoft", "W3School");  //Please visit W3School!
  • toUpperCase():把字符串转换为大写
var text1 = "Hello World!";       // 字符串
var text2 = text1.toUpperCase();  // text2 是被转换为大写的 text1
console.log(text2);  //HELLO WORLD!
  • toLowerCase():把字符串转换为小写
var text1 = "Hello World!";       // 字符串
var text2 = text1.toLowerCase();  // text2 是被转换为小写的 text1
console.log(text2);  //hello world!
  •  concat():连接两个或多个字符串
var text1 = "Hello";
var text2 = "World";
var text3 = text1.concat(" ", text2);
console.log(text3);  //Hello World
  • charAt():方法返回字符串中指定下标(位置)的字符串
var str = "HELLO WORLD";
var newStr = str.charAt(0);  // 返回 H
  • charCodeAt():方法返回字符串中指定索引的字符 unicode 编码
var str = "HELLO WORLD";
var newStr = str.charCodeAt(0);  //72
  •  split():将字符串转换为数组
var txt = "a,b,c,d,e";   // 字符串
var txt1 = txt.split(",");  // 用逗号分隔
  • includes():如果字符串包含指定值,返回 true
let text = "Hello world, welcome to the universe.";
console.log(text.includes("world"))   // 返回 true
let text = "Hello world, welcome to the universe.";
console.log(text.includes("world", 12))    // 返回 false
  •  startsWith():如果字符串以指定值开头,返回 true,否则返回 false(区分大小写)
let text = "Hello world, welcome to the universe.";
console.log(text.startsWith("Hello"))   // 返回 true
let text = "Hello world, welcome to the universe.";
console.log(text.startsWith("world", 6))    // 返回 false
  • endsWith()如果字符串以指定值结尾,返回 true,否则返回 false
var text = "Bill Gates";
text.endsWith("Gates")    // 返回 true

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小华仔仔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值