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 ...
随机推荐
- 作业帮在线业务 Kubernetes Serverless 虚拟节点大规模应用实践
简介:目前方案已经成熟,高峰期已有近万核规模的核心链路在线业务运行在基于阿里云 ACK+ECI 的 Kubernetes Serverless 虚拟节点.随着业务的放量,未来运行在 Serverle ...
- [Mobi] frida Hook 略知一二: frida-CLI, frida-server
Frida 是一款基于 python + javascript 的 hook 框架,主流平台都支持,由于是基于脚本的交互,因此相比 xposed 和 substrace cydia 更加便捷. 使用时 ...
- 本周ddl(4.1-4.5)
本周ddl(4.1-4.5) cs61a首先完成2.23之前的任务 cs61a完成3.1之前的学习 cs61a完成3.8任务并且ants完成阶段1 csapp的bomblab 记录南京5个景点及其周边 ...
- 一些有用的css函数
var 使用自定义的属性值. :root { --main-bg-color: pink; } body { background-color: var(--main-bg-color); } att ...
- Oracle、达梦:数据库大小写不敏感,但是又要区分大小写敏感(默认敏感)
一. 艹,这个需求就很操蛋. 实现 SELECT * FROM T1 WHERE REGEXP_LIKE(field, '.*value.*', 'c'); 在 Oracle 数据库中使用 REGEX ...
- Educational Codeforces Round 160 (Rated for Div. 2)
A 直接模拟,注意细节 #include<bits/stdc++.h> #define ll long long using namespace std; ll p[15] = {1}; ...
- 大模型_2.1:Prompt进阶
目录: 1.Prompt frameWork 2.Prompt 结构化格式 3.如何写好结构化 Prompt ? 4.Zero-Shot Prompts 5.Few-Shot Prompting 6. ...
- C#.NET体系图文概述—2024最全总结
C# 是一种简单.现代.面向对象和类型安全的编程语言.. .NET 是由 Microsoft 创建的开发平台,平台包含了语言规范.工具.运行,支持开发各种应用,如Web.移动.桌面等..NET框架有多 ...
- 使用.NET源生成器(SG)实现一个自动注入的生成器
DI依赖注入对我们后端程序员来说肯定是基础中的基础了,我们经常会使用下面的代码注入相关的service services.AddScoped<Biwen.AutoClassGen.TestCon ...
- 安装conda搭建python环境(保姆级教程)
参考文档: 安装conda搭建python环境(保姆级教程)