js中字符串的方法,17种方法
字符串的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种方法的更多相关文章
- JS中字符串倒序的两种方法
var reverse = function( str ){ var stack = [];//生成一个栈 for(var len = str.length,i=len;i>=0;i-- ){ ...
- js中判断数据类型的四种方法总结
js中判断数据类型的四种方法 前言 在js中,我们经常需要判断数据的类型,那么哪些方法可以用来判断数据的类型呢?哪种方法判断数据类型最准确呢? 我们来一个个分析: 1.typeof typeof是一个 ...
- js中数组去重的几种方法
js中数组去重的几种方法 1.遍历数组,一一比较,比较到相同的就删除后面的 function unique(arr){ ...
- Js中数据类型判断的几种方法
判断js中的数据类型有一下几种方法:typeof.instanceof. constructor. prototype. $.type()/jquery.type(),接下来主要比较一下这几种方法的异 ...
- 判断js中的数据类型的几种方法
判断js中的数据类型有一下几种方法:typeof.instanceof. constructor. prototype. $.type()/jquery.type(),接下来主要比较一下这几种方法的异 ...
- 转:判断js中的数据类型的几种方法
判断js中的数据类型有一下几种方法:typeof.instanceof. constructor. prototype. $.type()/jquery.type(),接下来主要比较一下这几种方法的异 ...
- JS 中检测数组的四种方法
今天和大家分享一下 JS 中检测是不是数组的四种方法,虽然篇幅不长,不过方法应该算是比较全面了. 1. instanceof 方法 instanceof 用于检测一个对象是不是某个类的实例,数组也是一 ...
- JS中检测数据类型的四种方法
1.typeof 用来检测数据类型的运算符->typeof value->返回值首先是一个字符串,其次里面包含了对应的数据类型,例如:"number"."st ...
- Python中字符串拼接的N种方法
python拼接字符串一般有以下几种方法: ①直接通过(+)操作符拼接 s = 'Hello'+' '+'World'+'!'print(s) 输出结果:Hello World! 使用这种方式进行字符 ...
- js中控制流管理的四种方法
引自http://es6.ruanyifeng.com/#docs/generator#yield--表达式 1.常用的回调方法 step1(function (value1) { step2(val ...
随机推荐
- Apsara Stack 技术百科 | 边缘场景智能云化,让云无处不在
简介:在过去十年间,随着计算技术的发展和移动互联网的广泛普及,各行业对数据本地计算和智能分析的需求与日俱增,越来越多的应用场景被接入了终端设备,导致终端侧的数据陡然增长,中心节点的处理算力不堪重负. ...
- dotnet 6 已知问题 ManualResetEventSlim 的 Set 方法抛出空异常
本文记录一个 dotnet 6 已知问题,此问题预计是在 .NET Framework 4.5 时就引入的,我没有考古在 .NET Framework 4.5 之前是否还存在此问题.当前这个问题在 . ...
- JUC并发编程学习笔记(十九)原子引用
原子引用 带版本号的原子操作! 解决ABA问题,引入原子引用(乐观锁思想) AtomicStampedReference类解决ABA问题 package org.example.cas; import ...
- 聊聊流言协议(Gossip)
什么是流言协议? 在分布式系统中,以下两个是典型的问题: 维护系统状态(节点的活跃性) 节点间的通信 解决这些问题的解决方案之一如下: 集中式状态管理服务 对等状态管理服务 集中式状态管理服务 像 A ...
- 模型评测-书生浦语大模型实战营学习笔记7&大语言模型12
大语言模型学习-12.模型评测 书生浦语大模型实战营学习笔记7 视频教程特别像广告,所以这篇博客参考了很多其他内容给大家参考,主要是下面几个页面: https://zhuanlan.zhihu.com ...
- GOLANG-配置nginx反向代理端口 配置域名
目录 配置/etc/nginx/nginx.conf文件 新建/etc/nginx/conf.d/doc.haimait.conf文件 重启nginx服务 解析自己的域名到服务器的公网ip 配置/et ...
- python教程6.4-json序列化
序列化:dumps,编码,将python类型转成json对象 反序列化:loads,解码,将json对象转成python对象 pickle 模块提供了四个功能:dumps.loads.dump.loa ...
- Hive笔记01
hive如何实现两个区域人均利润一样的时候,排名一样,出现1.2.2.3.4,这种重复排名的情况? 方案一 在Hive中,可以使用窗口函数和排名函数来实现重复排名的情况.具体步骤如下: 使用窗口函数计 ...
- IceRPC之深入理解调度管道->快乐的RPC
作者引言 很高兴啊,我们来到了IceRPC之深入理解调度管道->快乐的RPC,为上篇的续篇,深入理解常见的调度类型, 基础引导,有点小压力,打好基础,才能让自已不在迷茫,快乐的畅游世界. 传入请 ...
- 微信开发者工具拉取gitlab远程代码报Pull failed原因分析:
可能出现的原因: 本地主机上没有安装node node下载地址: 1 https://nodejs.org/zh-cn/download/ 没有保存gitlab的用户名和密码