目录
方法总结:
charAt() | 返回在指定位置的字符。 |
charCodeAt() | 返回在指定的位置的字符的 Unicode 编码。 |
concat() | 连接两个或更多字符串,并返回新的字符串。 |
fromCharCode() | 将 Unicode 编码转为字符。 |
indexOf() | 返回某个指定的字符串值在字符串中首次出现的位置。 |
includes() | 查找字符串中是否包含指定的子字符串。 |
lastIndexOf() | 从后向前搜索字符串,并从起始位置(0)开始计算返回字符串最后出现的位置。 |
match() | 查找找到一个或多个正则表达式的匹配。 |
replace() | 在字符串中查找匹配的子串,并替换与正则表达式匹配的子串。 |
replaceAll() | 在字符串中查找匹配的子串,并替换与正则表达式匹配的所有子串。 |
search() | 查找与正则表达式相匹配的值。 |
slice() | 提取字符串的片断,并在新的字符串中返回被提取的部分。 |
split() | 把字符串分割为字符串数组。 |
substr() | 从起始索引号提取字符串中指定数目的字符。 |
substring() | 提取字符串中两个指定的索引号之间的字符。 |
toLowerCase() | 把字符串转换为小写。 |
toUpperCase() | 把字符串转换为大写。 |
trim() | 去除字符串两边的空白。 |
toLocaleLowerCase() | 根据本地主机的语言环境把字符串转换为小写。 |
toLocaleUpperCase() | 根据本地主机的语言环境把字符串转换为大写。 |
valueOf() | 返回某个字符串对象的原始值。 |
toString() | 返回一个字符串。 |
代码案例总结:
charAt() 返回在指定位置的字符。
<script>
var str = "HELLO WORLD";
var n = str.charAt(2)
console.log(n) // L
</script>
charCodeAt() 返回在指定的位置的字符的 Unicode 编码。
<script>
var str = "HELLO WORLD";
var n = str.charCodeAt(0);
console.log(n) // 72
</script>
concat() 连接两个或更多字符串,并返回新的字符串。
<script>
var str1 = "Hello ";
var str2 = "world!";
var n = str1.concat(str2);
console.log(n) // Hello world!
</script>
fromCharCode() 将 Unicode 编码转为字符。
<script>
var n = String.fromCharCode(65);
console.log(n) // A
</script>
indexOf() 返回某个指定的字符串值在字符串中首次出现的位置。
<script>
var str = "Hello world, welcome to the universe.";
var n = str.indexOf("welcome");
console.log(n) // 13
</script>
includes() 查找字符串中是否包含指定的子字符串。
<script>
var str = "Hello world, welcome to the universe。";
var n = str.includes("universe");
console.log(n) // true
</script>
lastIndexOf() 从后向前搜索字符串,并从起始位置(0)开始计算返回字符串最后出现的位置。
<script>
var str = "I am from universe,welcome to universe site.";
var n = str.lastIndexOf("universe");
console.log(n) // 30
</script>
match() 查找找到一个或多个正则表达式的匹配。
<script>
var str = "The rain in SPAIN stays mainly in the plain";
var n = str.match(/ain/g);
console.log(n) // ['ain', 'ain', 'ain']
</script>
replace() 在字符串中查找匹配的子串,并替换与正则表达式匹配的子串。
<script>
var str = "Visit united! Visit united!";
var n = str.replace("united", "Runoob");
console.log(n) // Visit Runoob! Visit united!
</script>
replaceAll() 在字符串中查找匹配的子串,并替换与正则表达式匹配的所有子串。
<script>
var str = "Visit united! Visit united!";
var n = str.replaceAll("united", "Runoob");
console.log(n) // Visit Runoob! Visit Runoob!
</script>
search() 查找与正则表达式相匹配的值。
<script>
var str = "Visit united!";
var n = str.search("united");
console.log(n) // 6
</script>
slice() 提取字符串的片断,并在新的字符串中返回被提取的部分。
<script>
var str = "Hello world!";
var n = str.slice(1, 5);
console.log(n) // ello
</script>
split() 把字符串分割为字符串数组。
<script>
var str = "How are you doing today?";
var n = str.split(" ");
console.log(n) // ['How', 'are', 'you', 'doing', 'today?']
</script>
substr() 从起始索引号提取字符串中指定数目的字符。
<script>
var str = "Hello world!";
var n = str.substr(2, 3)
console.log(n) // llo
</script>
substring() 提取字符串中两个指定的索引号之间的字符。
<script>
var str = "Hello world!";
console.log(str.substring(3)) //lo world!
console.log(str.substring(3, 7)) // lo w
</script>
toLowerCase() 把字符串转换为小写。
<script>
var str = "United";
var res = str.toLocaleLowerCase();
console.log(res) // united
</script>
toUpperCase() 把字符串转换为大写。
<script>
var str = "united";
var res = str.toUpperCase(str);
console.log(res) // UNITED
</script>
trim() 去除字符串两边的空白。
<script>
var str = " united ";
console.log(str.trim()) // united
</script>
toLocaleLowerCase() 根据本地主机的语言环境把字符串转换为小写。
<script>
var str = "United";
var res = str.toLocaleLowerCase();
console.log(res) // united
</script>
toLocaleUpperCase() 根据本地主机的语言环境把字符串转换为大写。
<script>
var str = "united";
var res = str.toLocaleUpperCase();
console.log(res) // UNITED
</script>
valueOf() 返回某个字符串对象的原始值。
<script>
var str = "Hello world!";
console.log(str.valueOf()) // Hello world!
</script>
toString() 返回一个字符串。
<script>
var str = "united";
var res = str.toString();
console.log(res) // united
</script>