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 ...
随机推荐
- 前沿分享|阿里云资深技术专家 魏闯先:AnalyticDB PostgreSQL年度新版本发布
简介: 本篇内容为2021云栖大会-云原生数据仓库AnalyticDB技术与实践峰会分论坛中,阿里云资深技术专家 魏闯先关于"AnalyticDB PostgreSQL年度新版本发布&qu ...
- github只下载某个文件或文件夹(使用GitZip插件)
安装GitZip插件 (此安装过程需要梯子(不懂"梯子",百度一下就明白)) 1. 打开插件管理页面 方法一:打开Chrome浏览器(Edge浏览器同理),在Chrom地址栏输入c ...
- Solution Set - CDQ分治&整体二分
A[洛谷P2163].给定平面上若干个点,多次询问给定矩形内的点数. B[洛谷P3810].给定若干个三元组,对所有\(k\),求这样三元组的个数:恰有\(k\)个三元组,满足其每个分量都不超过它的相 ...
- Competition Set - AtCoder II
这里记录的是这个账号的比赛情况. ABC310 2023-7-15 Solved:6/8 1973->2053 七场ABC,两场打得蛮烂的,都因为AT炸掉Unrated了:另外五场全部满Perf ...
- ansible系列(27)--ansible的include任务复用
目录 1. include任务复用 1.1 多个项目调用相同task 1.2 Inlcude结合tags应用 1. include任务复用 有时,我们发现大量的 Playbook 内容需要重复编写,各 ...
- 前端JavaScript开发风格规范
开发者需要建立和遵守的规范 大致可以划分成这几个方向: 开发流程规范 代码规范 git commit规范 项目文件结构规范 UI设计规范 1. 开发流程规范 这里可能有小伙伴有疑问了,开发流程规范不是 ...
- Android Framework学习之系统启动流程
最近抽空看了framework一些内存,总结一下,留作后续回顾复习
- Nifi:Nifi中的Controller Service
Service简介 首先Nifi中的Controller Service 和我们MVC概念中的Controller Service不是一个概念,Nifi中的Controller Service更像是和 ...
- pageoffice6提取word指定位置(数据区域)的值
在实际的开发过程中,经常会遇到提取Word文档中指定位置的数据保存到数据库中的需求,PageOffice客户端控件即支持在线保存Word文件,也支持Word文档中的指定位置的数据或所有的数据提交到服务 ...
- java学习之旅(day.11)
static详解 static若在类中使用,就是修饰成员变量 static若在方法中使用,就是成员方法? static加在方法上叫静态方法,加在属性上叫做静态属性 package com.zhang. ...