字符串的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. 十年再出发,Dubbo 3.0 Preview 即将在 3 月发布

    ​简介:随着Dubbo和HSF的整合,我们在整个开源的体系中更多地去展现了 HSF 的能力,能够让更多的人通过使用 Dubbo 像阿里巴巴之前使用 HSF 一样更好的构建服务化的系统. 2011 年, ...

  2. [FAQ] JS 时间戳格式化为 date

    拷贝使用,不用引入第三方库 function formatDate (date = 0, fmt = 'yyyy-MM-dd hh:mm:ss') { date = new Date(+date) i ...

  3. IIncrementalGenerator 增量 Source Generator 生成代码入门 从语法到语义 获取类型完全限定名

    本文告诉大家如何在使用 IIncrementalGenerator 进行增量的 Source Generator 生成代码时,如何从语法分析过程,将获取的语法 Token 转换到语义分析上,比如获取类 ...

  4. dotnet 性能优化 利用哈希思想优化大对象集合相等判断性能

    利用哈希的其中一个思想,相同的对象的哈希值相同,可以用来提升一些大对象集合的进行对象相等判断的性能.大对象的相等判断指的是有某些类型的相等判断需要用到对象的很多属性或字段进行参与判断逻辑才能判断两个对 ...

  5. Django之ajax简介

    1.MTV与MVC 框架类型:MVC: M:models V:views C:controller Django用的框架就是MTV MTV: M:models T:templates V:views ...

  6. Spirng 当中 Bean的作用域

    Spirng 当中 Bean的作用域 @ 目录 Spirng 当中 Bean的作用域 每博一文案 1. Spring6 当中的 Bean的作用域 1.2 singleton 默认 1.3 protot ...

  7. 03. Ruby入门理解

    Ruby入门学习: 视频教程 https://www.bilibili.com/video/BV1QW411F7rh?t=401&p=1 笔记 https://github.com/haima ...

  8. Android开发环境配置 JDK及SDK

    已经搭建过无数次开发环境,今天把搭建环境记录下,下次不用去搜索别人博客,有些博客都是复制粘贴,有些关键信息都缺失了. 1.首先第一步:下载JDK,配置JDK环境变量.JDK可以在Oracle官网下载, ...

  9. 防止XSS(跨站脚本攻击)漏洞

    点击查看代码 - 输入验证和过滤:对于用户输入的数据,进行严格的验证和过滤.可以使用正则表达式或其他验证方式,确保输入的数据符合预期的格式和内容.同时,对于特殊字符进行转义处理,防止恶意代码的注入. ...

  10. WebGL:使用着色器进行几何造型

    前言 本文将介绍如何使用着色器来进行几何造型,说到几何图形大家一定都不陌生,比如说三角形.圆形,接触过WebGL基础使用的小伙伴一定都知道怎么去在画布上绘制一个三角形,只要传入三个顶点坐标,并选择绘图 ...