当前位置:网站首页>字符串方法以及实例
字符串方法以及实例
2022-07-24 05:21:00 【DniNgL】
字符串方法
var str = "VFDrerDAVvszds" //字面量方式声明字符串
// var str1=new String("sssssss") //构造函数方式声明字符串
console.log(str.length); // length 属性返回字符串的长度:13
- indexOf() 方法返回字符串中指定文本首次出现的索引(位置)
console.log(str.indexOf("s")) //indexOf() 方法返回字符串中指定文本首次出现的索引(位置)10
2.lastIndexOf() 方法返回指定文本在字符串中最后一次出现的索引
console.log(str.lastIndexOf("s")); // lastIndexOf()方法可返回一个指定的字符串值最后出现的位置
如果未找到所查找的元素, indexOf() 和 lastIndexOf() 均返回 -1
- search() 方法搜索特定值的字符串,并返回匹配的位置
console.log(str.search("s")); //搜索特定值的字符串,并返回匹配的位置
- slice(start,end) 提取字符串的某个部分并在新字符串中返回被提取的部分
console.log(str.slice(3, 5)); //裁剪字符串中位置 3到位置 5 的片段 :re
- substring(start,end) 提取字符串的某个部分并在新字符串中返回被提取的部分(无法接受负的索引)
console.log(str.substring(3,5));//裁剪字符串中位置 3到位置 5 的片段 :re
- substr(start, length)从start索引开始提取字符串的几个字符串
console.log(str.substr(3,5));//rerDA
- .replace() 方法用另一个值替换首个匹配的值,不会改变调用它的字符串。它返回的是新字符串
console.log(str.replace("V", "你")); //你FDrerDAVvszds
- toUpperCase() 把字符串转换为大写
console.log(str.toUpperCase());//VFDRERDAVVSZDS
- 通过 toLowerCase() 把字符串转换为小写
console.log(str.toLocaleLowerCase());//vfdrerdavvszds
- concat() 连接两个或多个字符串
var str1 = "hello",str2 = "javaScript"
var str3 = str1.concat(str2)
console.log(str3);//hellojavaScript
- trim() 方法删除字符串两端的空白符
var str=" swi "
var str1=str.trim()
console.log(str1);//swi
12.charAt() 方法返回字符串中指定下标(位置)的字符串
var str = "HELLO JAVASCRIPT";
str.charAt(0); // 返回 H
- 可以通过 split() 将字符串转换为数组
var txt = "a,b,c,d,e"; // 字符串
console.log(txt.split(",")); // ["a", "b", "c", "d", "e"] 用逗号分隔
以下是字符串实例
字符串方法将手机号码中间四位用*表示
<script>
var str = '12133536409'
function mosaic(str, start, length) {
let left = str.substring(0, start)
// console.log(left); //121
let middle = "*".repeat(length)
// console.log(middle); //****
let right = str.substring(start + length)
// console.log(right);//6409
return left + middle + right //将新的号码return出来
}
console.log(mosaic(str, 3, 4)); //121****6409
</script>
字符串长度截取
function cut(str, length) {
if (str.length < length) return str
else
return str.substr(0, length) + "..."
}
console.log(cut("你好,我是孙悟空", 5)); //你好,我是...
替换
let send = "尊敬的用户[0]您好,欢迎咨询我们的产品"
let numlist = "13344445555,12233334444,16677778888"
numlist.split(",").forEach(item => {
console.log(send.replace("[0]", item));
})
输入结果如下
边栏推荐
- Points for attention in adding spp module to the network
- How to quickly recover data after MySQL misoperation
- Authorized access to MySQL database
- 西瓜书/南瓜书--第1,2章总结
- Multi merchant mall system function disassembly lecture 12 - platform side commodity evaluation
- [vSphere high availability] host failover
- jestson安装ibus输入法
- Multi merchant mall system function disassembly lecture 09 - platform end commodity brands
- The way to attack the first poca hackson project "Manta network"
- likeshop单商户SAAS商城系统搭建,代码开源无加密。
猜你喜欢

tensorflow和pytorch框架的安装以及cuda踩坑记录

Multi merchant mall system function disassembly Lecture 14 - platform side member level

推荐一款完全开源,功能丰富,界面精美的商城系统

LSTM神经网络

Multi merchant mall system function disassembly lecture 04 - platform side merchants settling in

《统计学习方法(第2版)》李航 第16章 主成分分析 PCA 思维导图笔记 及 课后习题答案(步骤详细)PCA 矩阵奇异值 第十六章

Typora 安装包2021年11月最后一次免费版本的安装包下载V13.6.1

【深度学习】手写神经网络模型保存

Positional argument after keyword argument

Are you still trying to limit MySQL paging?
随机推荐
The female colleague of the company asked me to go to her residence to repair the computer at 11 o'clock at night. It turned out that disk C was popular. Look at my move to fix the female colleague's
Typora 安装包2021年11月最后一次免费版本的安装包下载V13.6.1
Problems in SSM project configuration, various dependencies, etc. (for personal use)
"Statistical learning methods (2nd Edition)" Li Hang Chapter 14 clustering method mind map notes and after-school exercise answers (detailed steps) K-means hierarchical clustering Chapter 14
《统计学习方法(第2版)》李航 第17章 潜在语义分析 LSA LSI 思维导图笔记 及 课后习题答案(步骤详细)第十七章
删除分类网络预训练权重的的head部分的权重以及修改权重名称
Chapter III summary of linear model
第四章 决策树总结
"Statistical learning methods (2nd Edition)" Li Hang Chapter 17 latent semantic analysis LSA LSI mind mapping notes and after-school exercise answers (detailed steps) Chapter 17
likeshop单商户商城系统搭建,代码开源无加密
第五章神经网络
Chapter IV decision tree summary
Target detection tagged data enhancement code
PDF文本合并
对ArrayList<ArrayList<Double>>排序
【activiti】activiti系统表说明
Subsystem technology and ecology may memorabilia | square one plan launched, Boca launched xcm!
LSTM神经网络
多商户商城系统功能拆解03讲-平台端商家管理
YOLOv5学习总结(持续更新)