本文最后更新于 2024-12-16T16:00:27+08:00
字符串截取
str.sclice(startIndex,[endIndex]) \(\Rightarrow\) 返回一个新的
startIndex~endIndex 之间的字符串
1 2 3 4 5 6 7 8 function sliceString ( ) { let string = "helloWorld" ; console .log ("slice==>" , string.slice (0 , 5 )); console .log ("slice==>" , string.slice (-2 , 5 )); console .log ("slice==>" , string.slice (-2 )); }sliceString ();
splice
str.substring(startIndex,[endIndex]) \(\Rightarrow\) 返回⼀个新的
startIndex~endIndex 之间的字符串 > [!note] > 当 startIndex
为负值时,把所有负值置为 0
1 2 3 4 5 6 7 8 9 function sliceString ( ) { let string = 'helloWorld' ; console .log ('substring==>' , string.substring (0 , 5 )); console .log ('substring==>' , string.substring (-4 )); console .log ('substring==>' , string.substring (-4 , 5 )); "orld" ] }sliceString ();
substring
str.split(xxx) \(\Rightarrow\) 以
xxx 为条件参数,返回⼀个全新的字符串不包含 xxx 这个参数 > [!note]
> 也可以理解成⽤ xxx 这个参数对字符串进⾏⼀个分割
1 2 3 4 5 6 7 function sliceString ( ) { let string = "hello World" ; console .log ("split==>" , string.split ("W" )); console .log ("split==>" , string.split (" " )); }sliceString ();
split
字符串大小写
1 2 3 4 5 6 7 8 function allUpperCase ( ) { const string = " hello Word " ; console .log ("s" , string.toUpperCase ()); console .log ("s" , string.toLowerCase ()); }toLowerCase ();
字符串去空格
str.trimStart() \(\Rightarrow\)
去除头部空格
1 2 3 4 5 6 function trimStartString ( ) { const string = " hello Word " ; console .log ("trimStart==>" , string.trimStart ()); }trimStartString ();
str.trim() \(\Rightarrow\)
去除两端空格
1 2 3 4 5 6 function trimString ( ) { const string = " hello Word " ; console .log ("trimStart==>" , string.trim ()); }trimString ();
str.trimEnd() \(\Rightarrow\)
去除尾部空格
1 2 3 4 5 6 function trimEndString ( ) { const string = " hello Word" ; console .log ("trimStart==>" , string.trimEnd ()); }trimEndString ();
trim
判断是否包含某个字符串(查找元素)
str.includes(val) \(\Rightarrow\)
判断是否包含 val 这个值
1 2 3 4 5 6 7 function isContainString (str, value ) { console .log ("includes==>" , str.includes (value)); }isContainString ("hello world app" , "o" );isContainString ("hello world app" , "s" );
str.charAt(val) \(\Rightarrow\)
根据索引输出对应的值 > [!note] > val 必须是正整数
1 2 3 4 5 6 7 8 9 function isContainString (str, value ) { console .log ("charAt==>" , str.charAt (value)); }isContainString ("hello world app" , "o" );isContainString ("hello world app" , 3 );isContainString ("hello world app" , -3 );
str.idexOf(val) \(\Rightarrow\)
根据 val 查找字符串第⼀个匹配索引 > [!note] > val
必须是字符串,有值为当前搜索值的第⼀个的索引;⽆值为-1
1 2 3 4 5 6 7 8 9 function isContainString (str, value ) { console .log ("indexOf==>" , str.indexOf (value)); }isContainString ("hello world app" , "o" );isContainString ("hello world app" , "h" );isContainString ("hello world app" , /[^\w\s]/g );
lastIndexOf(val) \(\Rightarrow\)
根据字符串获取最后一个索引 > [!note] >
参数必须为字符串,有值为当前搜索值的最后一个的索引;无值为-1
1 2 3 4 5 6 7 8 9 10 11 function isContainString (str, value ) { console .log ("lastIndexOf==>" , str.lastIndexOf (value)); }isContainString ("hello world app" , "o" );isContainString ("hello world app" , "h" );isContainString ("hello world app222" , 2 );isContainString ("hello world app222" , 3 );
search() \(\Rightarrow\)
根据字符串获取第一个索引 > [!note] > 参数可为正则或是字符串
1 2 3 4 5 6 7 8 9 10 11 function isContainString (str, value ) { console .log ("search==>" , str.search (value)); }isContainString ("hello world app" , "o" );isContainString ("hello world app" , "h" );isContainString ("hello world app" , 2 );isContainString ("hello world app" , /[^\h\s]/g );
startsWith() \(\Rightarrow\)
判断开头是否包含某个字符串,正确为 true,错误为 false
1 2 3 4 5 6 7 8 9 function isContainString (str, value ) { console .log ("startsWith==>" , str.startsWith (value)); }isContainString ("hello world app" , "o" );isContainString ("hello world app" , "h" );isContainString ("hello world app" , 2 );
endsWith() \(\Rightarrow\)
判断结尾是否包含某个字符串,正确为 true,错误为 false
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 function isContainString (str, value ) { console .log ("endsWith$Rightarrow$ " , str.endsWith ("app" )); }isContainString ("hello world app" , "o" );isContainString ("hello world app" , "h" );isContainString ("hello world app" , 2 );isContainString ("hello world app" , "app" );isContainString ("hello world app" , "p" );isContainString ("hello world app" , "d" );
拷⻉字符串
str.repeat(n) \(\Rightarrow\) 把
str 这个字符串重复 n 次,n 代表重复的次数
1 2 3 4 5 6 7 const dealString = (value ) => { const string = "hello app!" ; console .log ("repeat==>" , string.repeat ()); };dealString (2 );
替换字符串
str.replace(oldVal,newVal) \(\Rightarrow\) 把 newVal 替换成 oldVal
1 2 3 4 5 6 7 const dealString = ( ) => { const string = "hello app!" ; console .log ("replace==>" , string.replace ("app" , "world" )); };dealString ();
链接字符串
str.concat(str1,[str2,...]) \(\Rightarrow\)
将⼀个或多个字符串与原字符串连接合并,形成⼀个新的字符串并返 回
1 2 3 4 5 6 7 8 const dealString = ( ) => { const string = "hello app!" ; const a = "world" ; console .log ("concat==>" , string.concat (a)); };dealString ();
js之String的使用
https://www.gongyibai.site/String/