篇首语:本文由小编为大家整理,主要介绍了js字符串常用方法总结相关的知识,希望对你有一定的参考价值。
目录
1. trim()
2.toUpperCase()
3.toLowerCase()
4.concat()
5.replace()
6.slice()
7.includes
8.startsWidth
9.endsWidth
10.split
11.indexOf
1. trim()
trim去除两端字符串空白
trim()
方法返回一个从两头去掉空白字符的字符串,并不影响原字符串本身。
let str = " hello World " str = str.trim() console.log(str);//结果 hello World
2.toUpperCase()
方法将调用该方法的字符串转为大写形式并返回(如果调用该方法的值不是字符串类型会被强制转换)。
toUpperCase()
返回转为大写形式的字符串。此方法不会影响原字符串本身的值,因为javascript中字符串的值是不可改变的。
let str = "hello World" // str = str.trim() str = str.toUpperCase() console.log(str);//结果 HELLO WORLD
3.toLowerCase()
会将调用该方法的字符串值转为小写形式,并返回。
toLowerCase
会将调用该方法的字符串值转为小写形式,并返回。toLowerCase
不会影响字符串本身的值。
let str = "HELLO WORLD" // str = str.trim() // str = str.toUpperCase() str = str.toLowerCase()// 结果 hello world
4.concat()
concat
方法将一个或多个字符串与原字符串连接合并,形成一个新的字符串并返回。 concat
方法并不影响原字符串。
如果参数不是字符串类型,它们在连接之前将会被转换成字符串。
let str = "HELLO WORLD" // str = str.trim() // str = str.toUpperCase() // str = str.toLowerCase() str = str.concat("ao", ["teman"]) // HELLO WORLDaoteman
5.replace()
replace()
方法返回一个由替换值(replacement
)替换部分或所有的模式(pattern
)匹配项后的新字符串。模式可以是一个字符串或者一个正则表达式,替换值可以是一个字符串或者一个每次匹配都要调用的回调函数。如果pattern
是字符串,则仅替换第一个匹配项。原字符串不会改变。
1.指定替换字符串
let str = "hello world" str = str.replace("world", "sky")// 结果 hello sky
2.特殊符号替换
let str = "hello world hello world" str = str.replace("world", "$$") //插入一个 "$"。 // 结果 hello $ hello world str = str.replace("world", "$`") //插入当前匹配的子串左边的内容。 // 结果 hello hello hello world str = str.replace("world", "`$") // 插入当前匹配的子串右边的内容。 // 结果 hello hello hello world str = str.replace("world", "$\"") // 插入当前匹配的子串右边的内容。 // 结果 hello hello world hello world
3.指定一个函数作为参数
你可以指定一个函数作为第二个参数。在这种情况下,当匹配执行后,该函数就会执行。 函数的返回值作为替换字符串。 (注意:上面提到的特殊替换参数在这里不能被使用。) 另外要注意的是,如果第一个参数是正则表达式,并且其为全局匹配模式,那么这个方法将被多次调用,每次匹配都会被调用。
function styleHyphenFormat(propertyName) { return propertyName.replace(/[A-Z]/g, (match) => { return "-" + match.toLowerCase();// 转小写 }); } console.log(styleHyphenFormat("borderTop"));// 结果 border-top
6.slice()
slice()
方法提取某个字符串的一部分,并返回一个新的字符串,且不会改动原字符串。
语法:str.slice(beginIndex[, endIndex])
当只写一个参数时 会自动从第一个参数截取到后
当只写两个参数时,截取字符串 包右不包左
当参数为负数时 会从字符串的长度的length-参数 取后面的字符串
let str = "奥特曼打小怪兽" console.log(str.slice(3)); //打小怪兽 console.log(str.slice(3,5)); //打小 console.log(str.slice(-3)); //小怪兽 console.log(str.slice(-7,-4)); //小怪兽
7.includes
includes()
方法用于判断一个字符串是否包含在另一个字符串中,根据情况返回 true 或 false。
只能判断连续的字符串 不能判断间隔字符串的内容
includes() 方法是区分大小写的
如果是两个参数 第二个参数可以指出被搜寻字符串的索引
let str = "奥特曼打小怪兽abcd" console.log(str.includes("奥特曼"));//true console.log(str.includes("奥特打"));//false console.log(str.includes("奥特曼1"));//false console.log(str.includes("abcd"));// true console.log(str.includes("ABcd")); //false console.log(str.includes("奥",0));//true console.log(str.includes("奥特",0));//true console.log(str.includes("奥特",1));//false
8.startsWidth
startsWith()
方法用来判断当前字符串是否以另外一个给定的子字符串开头,并根据判断结果返回 true
或 false
。
语法:str.endsWith(searchString[, length]) searchString:要搜索的子字符串。 length:作为 str
的长度。默认值为 str.length
。
let str = "奥特曼打小怪兽" console.log(str.startsWith("奥")) // true console.log(str.startsWith("奥特曼")) //true console.log(str.startsWith("特曼")) //false console.log(str.startsWith("奥",0)) //true console.log(str.startsWith("奥",1)) //false console.log(str.startsWith("奥特",0)) //true
9.endsWidth
endsWith()
方法用来判断当前字符串是否是以另外一个给定的子字符串“结尾”的,根据判断结果返回 true
或 false
。
语法: str.startsWith(searchString[, position])
searchString:要搜索的子字符串。
position
:在 str
中搜索 searchString
的开始位置,默认值为 0。
let str = "奥特曼打小怪兽" console.log(str.endsWith("兽")) // true console.log(str.endsWith("小怪兽")) //true console.log(str.endsWith("奥")) //false console.log(str.endsWith("兽",1)) // fasle console.log(str.endsWith("兽",7)) // true
10.split
split()
方法使用指定的分隔符字符串将一个String
对象分割成子字符串数组,以一个指定的分割字串来决定每个拆分的位置。不会改变原字符串
语法: str.split([separator[, limit]])
separator:
指定表示每个拆分应发生的点的字符串。separator
可以是一个字符串或正则表达式。 如果纯文本分隔符包含多个字符,则必须找到整个字符串来表示分割点。如果在str中省略或不出现分隔符,则返回的数组包含一个由整个字符串组成的元素。如果分隔符为空字符串,则将str原字符串中每个字符的数组形式返回。
limit :
一个整数,限定返回的分割片段数量。当提供此参数时,split 方法会在指定分隔符的每次出现时分割该字符串,但在限制条目已放入数组时停止。如果在达到指定限制之前达到字符串的末尾,它可能仍然包含少于限制的条目。新数组中不返回剩下的文本。
let str = "奥特曼打小怪兽" console.log(str.split("")); //["奥", "特", "曼", "打", "小", "怪", "兽"] console.log(str.split("123")); //["奥特曼打小怪兽"] console.log(str.split("打")); // ["奥特曼", "小怪兽"] let str2 = "A奥A特A曼A打A小A怪A兽" console.log(str2.split("A")); //["", "奥", "特", "曼", "打", "小", "怪", "兽"] console.log(str2.split("A",2));// ["", "奥"]
11.indexOf
indexOf()
方法返回调用它的 String
对象中第一次出现的指定值的索引,从 fromIndex
处进行搜索。如果未找到该值,则返回 -1。字母区分大小写
str.indexOf(searchValue [, fromIndex])
searchValue:找到返回当前的索引 找不到返回-1
fromIndex
:数字表示开始查找的位置。可以是任意整数,默认值为 0
。
let str = "奥特曼打小怪兽,奥特曼打小怪兽" console.log(str.indexOf("奥")); //0 console.log(str.indexOf(",")); //7 console.log(str.indexOf("找不着")); //-1 console.log(str.indexOf("奥",2)); //8
12.toFixed
toFixed()
方法使用定点表示法来格式化一个数值。数值类型
let num = 123456.567 console.log(num.toFixed()); //123457 四舍五入 console.log(num.toFixed(2)); //123456.57
注意不是全部都可以四舍五入
let num2 = 2.55 console.log(num2.toFixed(1)); //2.5 let num3 = 2.56 console.log(num3.toFixed(1)); //2.6 四舍六入
以上是关于js字符串常用方法总结的主要内容,如果未能解决你的问题,请参考以下文章