JavaScript 字符串
什么是字符串?
- 字符串是用来存储和处理文本数据的
- 比如:
'一句话'
、'a'
等
- 比如:
字符串的创建方式
-
字面量方式进行创建
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> </head> <body></body> <script> // 字面量创建方式 // 双引号包含 var a = "123"; var b = "false"; console.log(typeof a); console.log(typeof b); </script> </html>
-
通过
new String
的方式进行创建<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> </head> <body></body> <script> // 通过 new String 的方式创建 var a = new String("123"); console.log(typeof a); // 对象 </script> </html>
字符串的长度 length
-
返回字符串的长度
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> </head> <body></body> <script> var a = "hello world"; console.log(a.length); // 11 </script> </html>
字符串的方法
-
charAt
返回指定位置上的内容,开始位置也是 0<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> </head> <body></body> <script> var a = "hello world"; var b = a.charAt(4); console.log(b); // o 返回第一个o </script> </html>
-
charCodeAt
返回指定位置上的 Unicode 值<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> </head> <body></body> <script> var a = "hello world"; var b = a.charCodeAt(1); // e // 返回字符串的 Unicode 值 console.log(b); // 101 </script> </html>
-
concant
连接两个或多个字符串<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> </head> <body></body> <script> var a = "hello world"; var b = "你"; var c = a.concat(b, "好!"); console.log(c); // hello world你好! </script> </html>
-
fromCharCode
将 Unicode 转换为字符串<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> </head> <body></body> <script> var a = String.fromCharCode(101); console.log(a); // e </script> </html>
-
indexOf
返回指定字符在字符串中出现的位置,找不到返回-1
-
lastIndexOf
返回指定字符在字符串中最后一次出现的位置,找不到返回-1
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> </head> <body></body> <script> var a = "hello world"; var b = a.indexOf("o"); console.log(b); // 4 var c = a.lastIndexOf("l"); console.log(c); // 9 </script> </html>
-
localeCompare
用本地特定的方式比较两个字符串的顺序,相同返回0
,不同返回-1
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> </head> <body></body> <script> var a = "hello"; var b = "hello"; var c = "world"; console.log(a.localeCompare(b)); // 0 console.log(a.localeCompare(c)); // -1 </script> </html>
-
replace
找到指定字符,并替换<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> </head> <body></body> <script> // 找到指定的字符并进行替换 var a = "hello world"; var b = a.replace("hello", ""); console.log(a); // hello world console.log(b); // world console.log(b.length); // 6 // 注意 world 前面的空格还在 </script> </html>
-
search
检索与正则表达式相匹配的值,能够检索到是0
,检索不到为-1
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> </head> <body></body> <script> var a = "hello world"; console.log(a.search("hello")); //0 console.log(a.search("hellox")); // -1 </script> </html>
-
slice
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> </head> <body></body> <script> var a = "hello"; // slice 第一个参数是开始的字符,第二个参数是剪切的个数(个数不是索引值) console.log(a.slice("h", 2)); // he </script> </html>
-
split
以指定的方式将字符串进行切割,并且返回数组<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> </head> <body></body> <script> var a = "hello world"; var c = "admin@13456"; var b = a.split(" "); var x = c.split("@"); console.log(b); // ['hello', 'world'] console.log(x); // ['admin', '13456'] </script> </html>