当前位置: 首页 > news >正文

JavaScript 字符串常用方法

文章目录

  • charAt(index): 返回指定索引处的字符
  • charCodeAt(index): 返回指定索引处字符的Unicode编码
  • concat(str1, str2, ..., strN): 连接两个或多个字符串,并返回新的字符串
  • includes(searchString, position): 判断一个字符串是否包含在另一个字符串中,根据情况返回true或false
  • indexOf(searchValue, fromIndex): 返回在字符串中首次找到指定值的索引,如果不存在则返回-1
  • lastIndexOf(searchValue, fromIndex): 返回指定值在字符串中最后一次出现的位置,如果不存在则返回-1
  • match(regexp): 使用正则表达式与字符串相匹配
  • replace(searchValue, newValue): 在字符串中查找匹配项并替换为新的值
  • search(regexp): 使用正则表达式搜索字符串,并返回匹配项的索引
  • slice(beginIndex, endIndex): 提取字符串的片段,并在新的字符串中返回被提取的部分
  • split(separator, limit): 通过将字符串分割成子字符串数组,来将一个字符串分割成字符串数组
  • substring(start, end): 提取字符串中两个指定的索引号之间的字符
  • toLowerCase()和toUpperCase(): 将字符串转换为小写/大写
  • toLocaleLowerCase() 和 toLocaleUpperCase():根据本地主机的语言环境将字符串转换为小写或大写。考虑了本地化的特殊字符
  • trim(): 去除字符串两端的空白字符
  • startsWith(searchString, position) 和endsWith(searchString, length):: 去除字符串两端的空白字符
  • substr(start, length): 从起始索引号提取指定长度的子字符串
  • repeat(count): 返回一个新字符串,该字符串包含将原字符串重复指定的次数后的结果
  • localeCompare(that, locales, options): 比较两个字符串,并返回用于表示两个字符串在排序顺序中的相对位置的数字
  • padStart(targetLength, padString) 和 padEnd(targetLength, padString): 在当前字符串的开始或末尾填充指定的字符串,直到达到目标长度。如果不指定填充字符串,则默认使用空格填充


charAt(index): 返回指定索引处的字符

let str = "Hello";  
console.log(str.charAt(1)); // 输出 "e"

charCodeAt(index): 返回指定索引处字符的Unicode编码

let str = "Hello";  
console.log(str.charCodeAt(1)); // 输出 101

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

let str1 = "Hello ";  
let str2 = "World!";  
console.log(str1.concat(str2)); // 输出 "Hello World!"

includes(searchString, position): 判断一个字符串是否包含在另一个字符串中,根据情况返回true或false

let str = "Hello World!";  
console.log(str.includes("World")); // 输出 true

indexOf(searchValue, fromIndex): 返回在字符串中首次找到指定值的索引,如果不存在则返回-1

let str = "Hello World!";  
console.log(str.indexOf("World")); // 输出 6

lastIndexOf(searchValue, fromIndex): 返回指定值在字符串中最后一次出现的位置,如果不存在则返回-1

let str = "Hello World! Hello!";  
console.log(str.lastIndexOf("Hello")); // 输出 13

match(regexp): 使用正则表达式与字符串相匹配

let str = "The quick brown fox jumps over 2 lazy dogs.";  
let regex = /\d+/g; // 匹配一个或多个数字  
let matches = str.match(regex);  
console.log(matches); // 输出: ["2"]

replace(searchValue, newValue): 在字符串中查找匹配项并替换为新的值

let str = "Hello World!";  
console.log(str.replace("World", "Vue")); // 输出 "Hello Vue!"

search(regexp): 使用正则表达式搜索字符串,并返回匹配项的索引

let str = "Hello World!";  
console.log(str.search(/World/)); // 输出 6

slice(beginIndex, endIndex): 提取字符串的片段,并在新的字符串中返回被提取的部分

let str = "Hello World!";  
console.log(str.slice(0, 5)); // 输出 "Hello"

split(separator, limit): 通过将字符串分割成子字符串数组,来将一个字符串分割成字符串数组

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

substring(start, end): 提取字符串中两个指定的索引号之间的字符

let str = "Hello World!";  
console.log(str.substring(0, 5)); // 输出 "Hello"

toLowerCase()和toUpperCase(): 将字符串转换为小写/大写

let str = "Hello World!";  
console.log(str.toLowerCase()); // 输出 "hello world!"
console.log(str.toUpperCase()); // 输出 "HELLO WORLD!"

toLocaleLowerCase() 和 toLocaleUpperCase():根据本地主机的语言环境将字符串转换为小写或大写。考虑了本地化的特殊字符

// 注意:这些方法的效果可能因本地环境而异  
let str = "STRİNG";  
console.log(str.toLocaleLowerCase('tr-TR')); // 在土耳其语环境中可能输出 "strİng"(I 保持大写,因为土耳其语中有大小写敏感的 I)  
console.log(str.toLocaleUpperCase('tr-TR')); // 在土耳其语环境中可能输出 "STRİNG"(保持不变)

trim(): 去除字符串两端的空白字符

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

startsWith(searchString, position) 和endsWith(searchString, length):: 去除字符串两端的空白字符

let str = "Hello World!";  
console.log(str.startsWith("Hello")); // 输出 true  
console.log(str.endsWith("!")); // 输出 true

substr(start, length): 从起始索引号提取指定长度的子字符串

let str = "Hello World!";  
console.log(str.substr(7, 5)); // 输出 "World"

repeat(count): 返回一个新字符串,该字符串包含将原字符串重复指定的次数后的结果

let str = "Hello";  
console.log(str.repeat(3)); // 输出 "HelloHelloHello"

localeCompare(that, locales, options): 比较两个字符串,并返回用于表示两个字符串在排序顺序中的相对位置的数字

let str1 = "apple";  
let str2 = "Banana";  
console.log(str1.localeCompare(str2)); // 输出 -1,因为 "apple" 在排序上位于 "Banana" 之前

padStart(targetLength, padString) 和 padEnd(targetLength, padString): 在当前字符串的开始或末尾填充指定的字符串,直到达到目标长度。如果不指定填充字符串,则默认使用空格填充

let str = "123";  
console.log(str.padStart(5, "0")); // 输出 "00123"  
console.log(str.padEnd(7, "!")); // 输出 "123!!!!"


http://www.mrgr.cn/news/54160.html

相关文章:

  • Virtuoso Layout无法显示元件,出现pcellEvalFailed错误问题解析
  • Code Review Item
  • 基于Multisim8路彩灯循环控制电路设计与仿真
  • springboot接口Get请求实体类入参
  • 引领智慧文旅新纪元,开启未来旅游新境界
  • 操作系统之内存管理基本概念
  • USART串口(发送和接收)
  • 【MySQL】多表查询——内连接,左/右连接
  • MyBatisPlus
  • 【算法】将单向链表按某值分成左边小、中间相等、右边大的形式
  • 在各大媒体报纸上刊登自己的文章用什么投稿方法发表快?
  • RDK X5 目标跟踪核心代码Deepsort
  • Flux.concat 使用说明书
  • 注塑机机械手升降传送机程序
  • 现代大数据架构Kappa
  • 【Java】—JavaBean转换方法详解
  • 数据结构练习题4(链表)
  • 网络空间安全之一个WH的超前沿全栈技术深入学习之路(二:渗透测试行业术语扫盲)作者——LJS
  • 单位评职称需要在指定媒体上投稿发表文章看我如何轻松应对
  • CGAL概述
  • 缓冲区类QBuffer
  • python-库
  • 【OD】【E卷】【真题】【100分】光伏场地建设规划(PythonJavajavaScriptC++C)
  • Chapter 2 - 7. Understanding Congestion in Fibre Channel Fabrics
  • mysql数据量分库分表
  • SOCKET与底层TCP协议的关系