JavaScript 字符串
1. 什么是字符串?
字符串是用来存储和处理文本数据,比如:
- 字符串可以存储一系列字符,如 “John Doe”。
- 字符串可以是插入到引号中的任何字符。你可以使用单引号或双引号:
2. 字符串的创建方式
-
字面量方式进行创建
-
<script> // 字面量创建方式 var a = "123"; var b = "false"; console.log(typeof a); // string console.log(typeof b); // string </script
-
-
通过 new String 的方式进行创建
-
<script> // 通过 new String() 的方式创建 var a = new String("hello"); console.log(typeof a); // object </script>
-
3. 字符串属性和方法
3.1 字符串属性
-
返回字符串的长度:length
-
<script> var a = "hello world"; var length = a.length; console.log(length); // 11 </script>
-
3.2 字符串方法
-
返回指定索引位置的字符:charAt()
-
<script> var a = "hello world"; var b = a.charAt(2); console.log(b); // 1 返回的是第一个l var c = a.charAt(4); console.log(c); // o </script>
-
-
返回指定索引位置字符的 Unicode 值:charCodeAt()
-
<script> var a = "hello world"; var b = a.charCodeAt(2); console.log(b); // 108 var week = "今天星期五"; var c = week.charCodeAt(0); console.log(c); // 20170 </script>
-
-
连接两个或多个字符串,返回连接后的字符串:concat()
-
<script> var a = "hello world"; var b = " JavaScript"; var c = a.concat(b); console.log(c); // hello world JavaScript </script>
-
-
将 Unicode 转化为字符串:fromCharCode()
-
<script> var a = String.fromCharCode(1024); console.log(a); // Ѐ </script>
-
-
返回字符串中检索指定字符第一次出现的位置:indexOf()
-
<script> var a = "hello world"; // 长度11 最大索引长度-1 索引是从0开始的 var aIndex = a.indexOf("l"); // 如果找不到则返回-1 console.log(aIndex); // 2 </script>
-
-
返回字符串中检索指定字符最后一次出现的位置:lastIndexOf()
-
<script> var a = "hello world"; // 长度11 最大索引长度-1 索引是从0开始的 var bIndex = a.lastIndexOf("l"); // 如果找不到则返回-1 console.log(bIndex); // 9 </script>
-
-
用本地特定的顺序来比较两个字符串,相同返回 0,不同返回 -1:localeCompare()
-
<script> var a = "hello"; var b = "hello"; var c = "hello world"; console.log(a.localeCompare(b)); // 0 console.log(a.localeCompare(c)); // -1 </script>
-
-
替换与正则表达式匹配的子串:replace()
-
<script> // 找到指定的字符 并进行替换 var a = "hello world"; var b = a.replace("hello", ""); console.log(b); // world console.log(b.length); // 6 console.log(a); // hello world </script>
-
-
检索与正则表达式相匹配的值:search()
-
<script> var a = "hello world"; var b = a.search("hello"); console.log(b); // 0 var c = a.search("hellox"); console.log(c); //-1 </script>
-
-
提取字符串的片段,并在新的字符串中返回被提取的部分:slice()
-
<script> var a = "hello"; console.log(a.slice(1, 4)); // ell 不包含第四个索引值 </script>
-
-
把字符串分割为字符串数组:split()
-
<script> var a = "hello world"; var b = a.split(" "); console.log(b); // ['hello', 'world'] var c = a.split(" w"); console.log(c); //['hello', 'orld'] var d = "admin@123456"; var e = d.split("@"); console.log(e); // ['admin', '123456'] </script>
-
-
从起始索引号提取字符串中指定数目的字符:substr()
-
<script> var a = "hello world"; // substr(index,num) index索引 num数量 var b = a.substr(a.indexOf("l"), 5); console.log(b); // llo w </script>
-
-
提取字符串中两个指定的索引号之间的字符:substring()
-
<script> var a = "hello world"; var indexA = a.indexOf("l"); var indexB = a.lastIndexOf("l"); var b = a.substring(indexA, indexB); console.log(b); // llo wor (不包含第二个索引值上的数) </script>
-
-
把字符串转换为小写:toLowerCase()
-
<script> var a = "HELLO WORLD"; var b = a.toLowerCase(); console.log(b); // hello world </script>
-
-
把字符串转换为大写:toUpperCase()
-
<script> var a = "hello world"; var b = a.toUpperCase(); console.log(b); // HELLO WORLD </script>
-
-
返回字符串对象值:toString()
-
<script> var a = new String("hello world"); console.log(a); // Object console.log(a.toString()); // hello world </script>
-
-
移除字符串首尾空白:trim()
-
<script> var a = " hello world "; var b = a.trim(); console.log(b); // hello world </script>
-
-
返回某个字符串对象的原始值:valueOf()
-
<script> var a = new String("hello world"); // Object console.log(a.valueOf()); // string </script>
-
本文详细介绍了JavaScript中的字符串,包括字符串的创建、常见属性和方法,如length、charAt、indexOf等。此外,还展示了如何进行字符串拼接、替换、搜索、分割等操作,帮助读者深入理解JavaScript字符串处理。
5704

被折叠的 条评论
为什么被折叠?



