js之String的使用

字符串截取

  • 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)); // hello
console.log("slice==>", string.slice(-2, 5)); // 无输出
console.log("slice==>", string.slice(-2)); // ld
}
// 运行sliceString()方法
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)); // hello
console.log('substring==>', string.substring(-4)); // helloWorld
console.log('substring==>', string.substring(-4, 5)); // hello
"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")); // ["hello", "orld"]
console.log("split==>", string.split(" ")); // ["hello", "World"]
}

sliceString();
split

字符串大小写

1
2
3
4
5
6
7
8
function allUpperCase() {
const string = " hello Word ";
// HELLO WORD
console.log("s", string.toUpperCase());
// hello word
console.log("s", string.toLowerCase());
}
toLowerCase();

字符串去空格

  • str.trimStart() \(\Rightarrow\) 去除头部空格
1
2
3
4
5
6
function trimStartString() {
const string = " hello Word ";
// hello Word
console.log("trimStart==>", string.trimStart());
}
trimStartString();
  • str.trim() \(\Rightarrow\) 去除两端空格
1
2
3
4
5
6
function trimString() {
const string = " hello Word ";
// hello Word
console.log("trimStart==>", string.trim());
}
trimString();
  • str.trimEnd() \(\Rightarrow\) 去除尾部空格
1
2
3
4
5
6
function trimEndString() {
const string = " hello Word";
// 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));
}
// true
isContainString("hello world app", "o");
// false
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));
}
// h
isContainString("hello world app", "o");
// l
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));
}
// 4
isContainString("hello world app", "o");
// 0
isContainString("hello world app", "h");
// -1
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));
}
// 7
isContainString("hello world app", "o");
// 0
isContainString("hello world app", "h");
// 17
isContainString("hello world app222", 2);
// -1
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));
}
// 4
isContainString("hello world app", "o");
// 0
isContainString("hello world app", "h");
// -1
isContainString("hello world app", 2);
// 1
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));
}
// false
isContainString("hello world app", "o");
// true
isContainString("hello world app", "h");
// false
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")); // false
}
// false
isContainString("hello world app", "o");
// false
isContainString("hello world app", "h");
// false
isContainString("hello world app", 2);
// true
isContainString("hello world app", "app");
// true
isContainString("hello world app", "p");
// false
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!";
// hello app!hello app!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!";
// hello world!
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";
// hello app!world
console.log("concat==>", string.concat(a));
};

dealString();

js之String的使用
https://www.gongyibai.site/String/
作者
爱吃糖醋排骨的苏小妍.
发布于
2024-11-27 10:14:09
更新于
2024-12-16 16:00:27
许可协议