字符串的17种方法。。。。。。

1.length:返回字符串的长度。

const str = "Hello, World!";
console.log(str.length); // 输出 13

2.charAt(index):返回指定索引位置的字符。

const str = "Hello, World!";
console.log(str.charAt(4)); // 输出 o

3.concat(str1, str2, ...):连接两个或多个字符串,并返回新的字符串。

const str1 = "Hello";
const str2 = "World";
const str3 = str1.concat(", ", str2);
console.log(str3); // 输出 Hello, World

4.indexOf(substring, start):返回子字符串在原字符串中第一次出现的位置,如果未找到则返回-1。

const str = "Hello, World!";
console.log(str.indexOf("World")); // 输出 7
console.log(str.indexOf("foo")); // 输出 -1
console.log(str.indexOf("Wor") !== -1); // true
// 位运算的方式还可以写成
console.log(~str.indexOf("Wor")); // -8
console.log(~str.indexOf("Wor1")); // 0

5.lastIndexOf(substring, start):返回子字符串在原字符串中最后一次出现的位置,如果未找到则返回-1。

const str = "Hello, World!";
console.log(str.lastIndexOf("o")); // 输出 8
console.log(str.lastIndexOf("foo")); // 输出 -1

6.slice(start, end):从原字符串中提取指定范围的字符,并返回新的字符串。

const str = "Hello, World!";
console.log(str.slice(7, 12)); // 输出 World

7.substring(start, end):从原字符串中提取指定范围的字符,并返回新的字符串。与 slice() 类似,但不支持负数参数。

const str = "Hello, World!";
console.log(str.substring(7, 12)); // 输出 World
console.log(str.substring(2)); // 输出 llo, World!

8.substr(start, length):从原字符串中提取指定长度的字符,并返回新的字符串。

const str = "Hello, World!";
console.log(str.substr(7, 5)); // 输出 World
console.log(str.substr(2, 3)); // 输出 llo

9.toLowerCase():将字符串转换为小写。

const str = "Hello, World!";
console.log(str.toLowerCase()); // 输出 hello, world!

10.toUpperCase():将字符串转换为大写。

const str = "Hello, World!";
console.log(str.toUpperCase()); // 输出 HELLO, WORLD!

11.trim():去除字符串两端的空格, 中间的空格不行。

const str = "   Hello, World!   ";
console.log(str.trim()); // 输出 Hello, World!

12.split(separator):将字符串按照指定的分隔符分割为数组。

const str = "Hello, World!";
const arr = str.split(", ");
console.log(arr); // 输出 ["Hello", "World!"]

13.replace(searchValue, replaceValue):将字符串中的指定内容替换为新的内容。

const str = "Hello, World!";
const newStr = str.replace("World", "Universe");
console.log(newStr); // 输出 Hello, Universe!

14.startsWith(searchString, position):判断字符串是否以指定的子字符串开头。

const str = "Hello, World!";
console.log(str.startsWith("Hello")); // 输出 true
console.log(str.startsWith("World")); // 输出 false

15.endsWith(searchString, position):判断字符串是否以指定的子字符串结尾。

const str = "Hello, World!";
console.log(str.endsWith("World!")); // 输出 true
console.log(str.endsWith("Hello")); // 输出 false

16.includes(searchString, position):判断字符串是否包含指定的子字符串。

const str = "Hello, World!";
console.log(str.includes("World")); // 输出 true
console.log(str.includes("foo")); // 输出 false

17.match(regexp):通过正则表达式在字符串中搜索匹配项,并返回匹配结果的数组。

const str = "Hello, World!";
const matches = str.match(/[a-zA-Z]+/g);
console.log(matches); // 输出 ["Hello", "World"] const str1 = "Hello,12131,a23,232,232 World!";
const matches1 = str1.match(/[a-zA-Z]+/g);
console.log(matches1); // 输出 ['Hello', 'a', 'World']

js中字符串的方法,17种方法的更多相关文章

  1. JS中字符串倒序的两种方法

    var reverse = function( str ){ var stack = [];//生成一个栈 for(var len = str.length,i=len;i>=0;i-- ){ ...

  2. js中判断数据类型的四种方法总结

    js中判断数据类型的四种方法 前言 在js中,我们经常需要判断数据的类型,那么哪些方法可以用来判断数据的类型呢?哪种方法判断数据类型最准确呢? 我们来一个个分析: 1.typeof typeof是一个 ...

  3. js中数组去重的几种方法

    js中数组去重的几种方法         1.遍历数组,一一比较,比较到相同的就删除后面的                 function unique(arr){                 ...

  4. Js中数据类型判断的几种方法

    判断js中的数据类型有一下几种方法:typeof.instanceof. constructor. prototype. $.type()/jquery.type(),接下来主要比较一下这几种方法的异 ...

  5. 判断js中的数据类型的几种方法

    判断js中的数据类型有一下几种方法:typeof.instanceof. constructor. prototype. $.type()/jquery.type(),接下来主要比较一下这几种方法的异 ...

  6. 转:判断js中的数据类型的几种方法

    判断js中的数据类型有一下几种方法:typeof.instanceof. constructor. prototype. $.type()/jquery.type(),接下来主要比较一下这几种方法的异 ...

  7. JS 中检测数组的四种方法

    今天和大家分享一下 JS 中检测是不是数组的四种方法,虽然篇幅不长,不过方法应该算是比较全面了. 1. instanceof 方法 instanceof 用于检测一个对象是不是某个类的实例,数组也是一 ...

  8. JS中检测数据类型的四种方法

    1.typeof 用来检测数据类型的运算符->typeof value->返回值首先是一个字符串,其次里面包含了对应的数据类型,例如:"number"."st ...

  9. Python中字符串拼接的N种方法

    python拼接字符串一般有以下几种方法: ①直接通过(+)操作符拼接 s = 'Hello'+' '+'World'+'!'print(s) 输出结果:Hello World! 使用这种方式进行字符 ...

  10. js中控制流管理的四种方法

    引自http://es6.ruanyifeng.com/#docs/generator#yield--表达式 1.常用的回调方法 step1(function (value1) { step2(val ...

随机推荐

  1. go语言package使用

    近期接触go感觉package包之间引用很麻烦,很绕圈子.下面一起理一理这个package咋用 关于package: 1.不限于一个文件,可以多个文件组成一个package 2.不要求package的 ...

  2. XOps笔记

    当前是 Ops盛行的时代,在互联网圈内的你一定经常都会听到这些名词,DevOps.DevSecOps.GitOps.NetOps.ItOps.Aiops.DataOps.MLOps.NoOps:无论是 ...

  3. GitHub总是打不开

    终极解决方案 http://tool.chinaz.com/dns?type=1&host=github.com&ip= github.com vim /etc/hosts 添加下面内 ...

  4. LVS负载均衡(4)-- LVS FWM防火墙标记

    防火墙标记的作用是:借助于防火墙标记来分类报文,然后基于标记定义集群服务:可将多个不同的应用使用同一个集群服务进行调度. 实现方法: 在Director主机打标记,作用在mangle表的PREROUT ...

  5. Android Framework学习之系统启动流程

    最近抽空看了framework一些内存,总结一下,留作后续回顾复习

  6. 远程控制软件 TeamViewer 的局限性和替代方案

    TeamViewer 公司创建于2005年,总部位于德国,客户遍及全球,其中企业用户居多,其各方面性能都很不错,但价格却非常贵.针对个人用户,TeamViewer 提供免费版软件,但时不时会提示&qu ...

  7. IPv6 — 子网划分

    目录 文章目录 目录 前文列表 IPv6 的子网划分 前文列表 <IPv6 - 网际协议第 6 版> <IPv6 - 地址格式与寻址模式> <IPv6 - 协议头> ...

  8. C 语言编程 — 编程实践

    目录 文章目录 目录 前文列表 程序示例 前文列表 <程序编译流程与 GCC 编译器> <C 语言编程 - 基本语法> <C 语言编程 - 基本数据类型> < ...

  9. C 语言编程 — 堆栈与内存管理

    目录 文章目录 目录 前文列表 栈(Stack)和堆(Heap) 栈 堆 内存管理 动态分配内存 重新调整内存的大小和释放内存 前文列表 <程序编译流程与 GCC 编译器> <C 语 ...

  10. Chrome:用uBlacklist屏蔽CSDN搜索结果

    CSDN现在广告满天飞,且很多博客需要先关注才能复制,非常令人无语.如果每次用Google搜索的时候都要加上"-csdn"选项,就非常麻烦.有没有更方便的办法呢?我们可以利用Chr ...