首页 > 其他分享 >js中字符串的方法

js中字符串的方法

时间:2023-07-11 16:24:07浏览次数:21  
标签:console log js str 字符串 World 方法 Hello const

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

length:返回字符串的长度。

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

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

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

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

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

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

const str = "Hello, World!";
console.log(str.indexOf("World")); // 输出 7
console.log(str.indexOf("foo")); // 输出 -1
console.log(str.indexOf("foo") === -1); // true

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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']

 

标签:console,log,js,str,字符串,World,方法,Hello,const
From: https://www.cnblogs.com/dreamtt/p/17544631.html

相关文章

  • 修改pip install默认安装路径的方法
    1.修改pipinstall默认安装路径一般使用Anaconda时会使用pipinstall###来安装各类包,但默认安装路径在C盘,极大占用空间,作为强迫症,我们通过以下步骤来修改默认安装路径。 1.1、查看pip默认安装位置第一步:通过win菜单,Anaconda3找到Prompt,点击进入python-msite获......
  • nvm安装node报错Get "https://nodejs.org/dist/latest/SHASUMS256.txt": dial tcp 104
    windows上通过nvm管理node版本,在本地安装了nvm后,通过nvm安装node,报错了,信息:Couldnotretrievehttps://nodejs.org/dist/latest/SHASUMS256.txt.Gethttps://nodejs.org/dist/latest/SHASUMS256.txt:dialtcp104.20.23.46:443:i/otimeout 有了这样的信息,我......
  • 99.如何在不使用额外空间的情况下,交换两个数?你有几种方法
    99.如何在不使用额外空间的情况下,交换两个数?你有几种方法1)算术x=x+y;y=x-y;x=x-y;2)异或x=x^y;//只能对int,char..y=x^y;x=x^y;x^=y^=x;参考资料来源:阿秀......
  • RF2.4G 调节色温、亮度的方法
    1.发射端发出定义字节的数据。case8://色温加 {if(step>5){RemoteCmd[1]=0x6A;RemoteCmd[7]=0x80; KeyDownNoSendEn=0;//按键按下但不发送RF指令失能 KeyReleaseSendEn=0; UpdataKeyReleaseSendBufEn=0;}else{RemoteCmd[1]......
  • echarts5.0 去掉 hover 折线变粗方法
    问题今天echarts版本更新至5.1.2,发现鼠标悬停在折线图上,折线变粗,这个效果和我的需求相悖,所以想办法去掉此效果解决emphasis:折线图的高亮状态。设置emphasis.lineStyle和折线图本身lineStyle相同即可letoption={ ....//此处为其他配置 series:[ { type:"line",......
  • Qt QJsonDocument以及与QJsonArray、QJsonObject、QJsonValue的关联
    0、说明QJsonDocument类提供了read/writeJSON文档的方法。用QJsonDocument::fromJson()方法,可以从将一个JSON文件(或者QByteArray数据)转换为QJsonDocument,用QJsonDocument::toJson()则能起到相反的用法。在此过程中的语法解析是很高效的,并且可以将JSON转换为Qt使用的二......
  • 基于生长的棋盘格角点检测方法--(1)原理介绍
    前言棋盘格中角点检测方法是相机标定中必不可少的步骤之一。Opencv中的函数boolfindChessboardCorners(InputArrayimage,SizepatternSize,OutputArraycorners,intflags=CALIB_CB_ADAPTIVE_THRESH+CALIB_CB_NORMALIZE_IMAGE)就可以轻松实现棋盘格角点检测结果。如下图所示......
  • 基于生长的棋盘格角点检测方法--(2)代码详解(上)
    上一篇介绍了基于生长的棋盘格角点检测方法的大概原理,详见:基于生长的棋盘格角点检测方法–(1)原理介绍本文进一步从代码解读角度出发,更深入地理解工程中是如何实现的。本文中用到的代码可以从以下链接下载http://www.cvlibs.net/software/libcbdetect/这里我把代码中主要的函......
  • 基于生长的棋盘格角点检测方法--(3)代码详解(下)
    接着上一篇基于生长的棋盘格角点检测方法–(2)代码详解(上),来看一下第二个重要函数chessboardsFromCorners。该函数的目的是用上一步骤中找到的角点恢复出棋盘结构。首先初始化一个3x3的角点矩阵,也就是一个2x2的棋盘格,这是组成一个棋盘的最小单位了。然后利用定义的棋盘能量函数来从......
  • js 检测文本是否溢出
    自定义指令的方式手写实现/***检测文本是否溢出*参考https://github.com/ElemeFE/element/blob/dev/packages/table/src/table-body.js#L241*@param{*}e*@returns*/functionisEllipsis(e){returnnewPromise(resolve=>{constel=window.event......