【javascript】javascript字符串函数 总结

目录

charAt()

concat()

indexOf()

substring()

replace()

toLowerCase()

toUpperCase()

split()

trim()

startsWith()

endsWith()

includes()


charAt()

作用:返回指定位置的字符。

const str = "Hello";
const char = str.charAt(1);
console.log(char); // e

concat()

作用:将两个或多个字符串连接起来。

const str1 = "Hello";
const str2 = "World";
const newStr = str1.concat(str2);
console.log(newStr); //HelloWorld

indexOf()

作用:返回指定字符或子字符串在字符串中首次出现的位置。

const str = "Hello World";
const index = str.indexOf("o");
console.log(index); //4

substring()

作用:提取字符串中介于两个指定下标之间的字符。

const str = "Hello World";
const substring = str.substring(6, 11);
console.log(substring); // World

replace()

作用:将字符串中指定的字符或子字符串替换为新的字符或子字符串。

const str = "Hello World";
const newStr = str.replace("World", "Universe");
console.log(newStr); // Hello Universe

toLowerCase()

作用:将字符串中的字母转换为小写。

const str = "Hello World";
const lowerCaseStr = str.toLowerCase();
console.log(lowerCaseStr); // hello world
 

toUpperCase()

作用:将字符串中的字母转换为大写。

const str = "Hello World";
const upperCaseStr = str.toUpperCase();
console.log(upperCaseStr);// HELLO WORLD

split()

作用:将字符串分割为字符串数组。

const str = "Hello,World";
const arr = str.split(",");
console.log(arr); // ["Hello", "World"]

trim()

作用:去除字符串两端的空格。

const str = "   Hello World   ";
const trimmedStr = str.trim();
console.log(trimmedStr); // Hello World

startsWith()

作用:判断字符串是否以指定的字符开头。

const str = "Hello World";
const startsWithHello = str.startsWith("Hello");
console.log(startsWithHello); // true

endsWith()

作用:判断字符串是否以指定的字符结尾。

const str = "Hello World";
const endsWithWorld = str.endsWith("World");
console.log(endsWithWorld); // true

includes()

作用:判断字符串是否包含指定的字符或子字符串。

const str = "Hello World";
const includesHello = str.includes("Hello");
console.log(includesHello); // true

猜你喜欢

转载自blog.csdn.net/m0_64494670/article/details/131463713