Javascript字符串的常用方法

这里增的意思并不是说直接增添内容,而是创建字符串的一个副本,再进行操作

除了常用+以及${}进行字符串拼接之外,还可通过concat

concat

用于将一个或多个字符串拼接成一个新字符串

javascript 复制代码
let stringValue = "hello ";
let result = stringValue.concat("world");
console.log(result); // "hello world"
console.log(stringValue); // "hello"

这里的删的意思并不是说删除原字符串的内容,而是创建字符串的一个副本,再进行操作

常见的有:

  • slice()
  • substr()
  • substring()

这三个方法都返回调用它们的字符串的一个子字符串,而且都接收一或两个参数。

javascript 复制代码
let stringValue = "hello world";
console.log(stringValue.slice(3)); // "lo world"
console.log(stringValue.substring(3)); // "lo world"
console.log(stringValue.substr(3)); // "lo world"
console.log(stringValue.slice(3, 7)); // "lo w"
console.log(stringValue.substring(3,7)); // "lo w"
console.log(stringValue.substr(3, 7)); // "lo worl"

这里改的意思也不是改变原字符串,而是创建字符串的一个副本,再进行操作

常见的有:

  • trim()、trimLeft()、trimRight()
  • repeat()
  • padStart()、padEnd()
  • toLowerCase()、 toUpperCase()

trim()、trimLeft()、trimRight()

删除前、后或前后所有空格符,再返回新的字符串

javascript 复制代码
let stringValue = " hello world ";
let trimmedStringValue = stringValue.trim();
console.log(stringValue); // " hello world "
console.log(trimmedStringValue); // "hello world"

repeat()

接收一个整数参数,表示要将字符串复制多少次,然后返回拼接所有副本后的结果

javascript 复制代码
let stringValue = "na ";
let copyResult = stringValue.repeat(2) // na na

padEnd()

复制字符串,如果小于指定长度,则在相应一边填充字符,直至满足长度条件

javascript 复制代码
let stringValue = "foo";
console.log(stringValue.padStart(6)); // " foo"
console.log(stringValue.padStart(9, ".")); // "......foo"

toLowerCase()、 toUpperCase()

大小写转化

javascript 复制代码
let stringValue = "hello world";
console.log(stringValue.toUpperCase()); // "HELLO WORLD"
console.log(stringValue.toLowerCase()); // "hello world"

除了通过索引的方式获取字符串的值,还可通过:

  • chatAt()
  • indexOf()
  • startWith()
  • includes()

charAt()

返回给定索引位置的字符,由传给方法的整数参数指定

javascript 复制代码
let message = "abcde";
console.log(message.charAt(2)); // "c"

indexOf()

从字符串开头去搜索传入的字符串,并返回位置(如果没找到,则返回 -1 )

javascript 复制代码
let stringValue = "hello world";
console.log(stringValue.indexOf("o")); // 4

startWith()、includes()

从字符串中搜索传入的字符串,并返回一个表示是否包含的布尔值

javascript 复制代码
let message = "foobarbaz";
console.log(message.startsWith("foo")); // true
console.log(message.startsWith("bar")); // false
console.log(message.includes("bar")); // true
console.log(message.includes("qux")); // false

转换方法

split

把字符串按照指定的分割符,拆分成数组中的每一项

javascript 复制代码
let str = "12+23+34"
let arr = str.split("+") // [12,23,34]

模板匹配方法

针对正则表达式,字符串设计了几个方法:

  • match()
  • search()
  • replace()

match()

接收一个参数,可以是一个正则表达式字符串,也可以是一个RegExp对象,返回数组

javascript 复制代码
let text = "cat, bat, sat, fat";
let pattern = /.at/;
let matches = text.match(pattern);
console.log(matches[0]); // "cat"

search()

接收一个参数,可以是一个正则表达式字符串,也可以是一个RegExp对象,找到则返回匹配索引,否则返回 -1

javascript 复制代码
let text = "cat, bat, sat, fat";
let pos = text.search(/at/);
console.log(pos); // 1

replace()

接收两个参数,第一个参数为匹配的内容,第二个参数为替换的元素(可用函数)

javascript 复制代码
let text = "cat, bat, sat, fat";
let result = text.replace("at", "ond");
console.log(result); // "cond, bat, sat, fat"
相关推荐
gnip1 分钟前
工程项目中.env 文件原理
前端·javascript
☺����2 分钟前
实现自己的AI视频监控系统-第一章-视频拉流与解码2
开发语言·人工智能·python·音视频
JefferyXZF20 分钟前
Next.js Server Actions 详解: 无缝衔接前后端的革命性技术(八)
前端·全栈·next.js
染翰21 分钟前
lua入门以及在Redis中的应用
开发语言·redis·lua
王者鳜錸32 分钟前
PYTHON让繁琐的工作自动化-函数
开发语言·python·自动化
JohnYan1 小时前
工作笔记 - CentOS7环境运行Bun应用
javascript·后端·容器
兔老大RabbitMQ1 小时前
git pull origin master失败
java·开发语言·git
tt5555555555551 小时前
C/C++嵌入式笔试核心考点精解
c语言·开发语言·c++
xiao助阵1 小时前
python实现梅尔频率倒谱系数(MFCC) 除了傅里叶变换和离散余弦变换
开发语言·python
芜青2 小时前
HTML+CSS:浮动详解
前端·css·html