小白教程
所有教程
关于
Search
172.70.42.153
172.70.42.153
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“JavaScript 字符串(String)对象”的源代码
本页内容
上一节:
JavaScript_Number_对象
下一节:
JavaScript_Date(日期)对象
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:JavaScript String}}[[Category:Javascript 教程|60]] = JavaScript 字符串(String) 对象 = String 对象用于处理已有的字符块。 == JavaScript 字符串 == 一个字符串用于存储一系列字符就像 "John Doe". 一个字符串可以使用单引号或双引号: <sample title="" desc="" lang="html" hererun="1"> var carname="Volvo XC60"; var carname='Volvo XC60'; </sample> 你使用位置(索引)可以访问字符串中任何的字符: <sample title="" desc="" lang="html" hererun="1"> var character=carname[7]; </sample> 字符串的索引从零开始, 所以字符串第一字符为 [0],第二个字符为 [1], 等等。 你可以在字符串中使用引号,如下实例: <sample title="" desc="" lang="html" hererun="1"> var answer="It's alright"; var answer="He is called 'Johnny'"; var answer='He is called "Johnny"'; </sample> 或者你可以在字符串中使用转义字符(\)使用引号: <sample title="" desc="" lang="html" hererun="1"> var answer='It\'s alright'; var answer="He is called \"Johnny\""; </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <script> var carname1="Volvo XC60"; var carname2='Volvo XC60'; var answer1='It\'s alright'; var answer2="He is called \"Johnny\""; var answer3='He is called "Johnny"'; document.write(carname1 + "<br>") document.write(carname2 + "<br>") document.write(answer1 + "<br>") document.write(answer2 + "<br>") document.write(answer3 + "<br>") </script> </body> </html> </run> == 字符串(String) == 字符串(String)使用长度属性'''length'''来计算字符串的长度: <sample title="" desc="" lang="html" hererun="1"> var txt="Hello World!"; document.write(txt.length); var txt="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; document.write(txt.length); </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <script> var txt = "Hello World!"; document.write("<p>" + txt.length + "</p>"); var txt="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; document.write("<p>" + txt.length + "</p>"); </script> </body> </html> </run> == 在字符串中查找字符串 == 字符串使用 indexOf() 来定位字符串中某一个指定的字符首次出现的位置: <sample title="" desc="" lang="html" hererun="1"> var str="Hello world, welcome to the universe."; var n=str.indexOf("welcome"); </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <p id="p1">Click the button to locate where "locate" first occurs.</p> <p id="p2">0</p> <button onclick="myFunction()">点我</button> <script> function myFunction(){ var str=document.getElementById("p1").innerHTML; var n=str.indexOf("locate"); document.getElementById("p2").innerHTML=n+1; } </script> </body> </html> </run> 如果没找到对应的字符函数返回-1 lastIndexOf() 方法在字符串末尾开始查找字符串出现的位置。 == 内容匹配 == '''match()'''函数用来查找字符串中特定的字符,并且如果找到的话,则返回这个字符。 <sample title="" desc="" lang="html" hererun="1"> var str="Hello world!"; document.write(str.match("world") + "<br>"); document.write(str.match("World") + "<br>"); document.write(str.match("world!")); </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <script> var str="Hello world!"; document.write(str.match("world") + "<br>"); document.write(str.match("World") + "<br>"); document.write(str.match("world!")); </script> </body> </html> </run> == 替换内容 == '''replace()''' 方法在字符串中用某些字符替换另一些字符。 <sample title="" desc="" lang="html" hererun="1"> str="Please visit Microsoft!" var n=str.replace("Microsoft","xiaoBai"); </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <p>替换 "Microsoft" 为 "xiaoBai" :</p> <button onclick="myFunction()">点我</button> <p id="demo">请访问 Microsoft!</p> <script> function myFunction() { var str = document.getElementById("demo").innerHTML; var txt = str.replace("Microsoft","xiaoBai"); document.getElementById("demo").innerHTML = txt; } </script> </body> </html> </run> == 字符串大小写转换 == 字符串大小写转换使用函数 '''toUpperCase()''' / '''toLowerCase()''': <sample title="" desc="" lang="html" hererun="1"> var txt="Hello World!"; // String var txt1=txt.toUpperCase(); // txt1 文本会转换为大写 var txt2=txt.toLowerCase(); // txt2 文本会转换为小写 </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <script> var txt="Hello World!"; document.write("<p>" + txt.toUpperCase() + "</p>"); document.write("<p>" + txt.toLowerCase() + "</p>"); document.write("<p>" + txt + "</p>"); </script> <p>该方法返回一个新的字符串,源字符串没有被改变。</p> </body> </html> </run> == 字符串转为数组 == 字符串使用'''split()'''函数转为数组: <sample title="" desc="" lang="html" hererun="1"> txt="a,b,c,d,e" // String txt.split(","); // 使用逗号分隔 txt.split(" "); // 使用空格分隔 txt.split("|"); // 使用竖线分隔 </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <p id="demo">单击按钮显示数组。</p> <button onclick="myFunction()">点我</button> <script> function myFunction(){ var str="a,b,c,d,e,f"; var n=str.split(","); document.getElementById("demo").innerHTML=n[0]; } </script> </body> </html> </run> == 特殊字符 == Javascript 中可以使用反斜线(\)插入特殊符号,如:撇号,引号等其他特殊符号。 查看如下 JavaScript 代码: <pre> var txt="We are the so-called "Vikings" from the north."; document.write(txt);</pre> 在JavaScript中,字符串的开始和停止使用单引号或双引号。这意味着,上面的字符串将被切成: We are the so-called 解决以上的问题可以使用反斜线来转义引号: var txt="We are the so-called \"Vikings\" from the north."; document.write(txt); JavaScript将输出正确的文本字符串:We are the so-called "Vikings" from the north. 下表列出其他特殊字符,可以使用反斜线转义特殊字符: {| class="table table-striped table-hover" ! 代码 ! 输出 |- | \' | 单引号 |- | \" | 双引号 |- | \\ | 斜杆 |- | \n | 换行 |- | \r | 回车 |- | \t | tab |- | \b | 空格 |- | \f | 换页 |} == 字符串属性和方法 == 属性: * length * prototype * constructor 方法: * charAt() * charCodeAt() * concat() * fromCharCode() * indexOf() * lastIndexOf() * match() * replace() * search() * slice() * split() * substr() * substring() * toLowerCase() * toUpperCase() * valueOf() 更多方法与属性介绍可以参考:[[jsref obj string|JavaScript String 对象]]
返回至“
JavaScript 字符串(String)对象
”。
上一节:
JavaScript_Number_对象
下一节:
JavaScript_Date(日期)对象