?????? ??? ??
??? ?? ??
JavaScript ??? ??? ??? ???? ???? ? ?????. String ??? ???? ??? ??? ????.
<script language="JavaScript"> var str_object = new String( str ); var str1 = String( str ); var str2 = str; </script>
?? ? ?? ?? ? ? ?? ??? String ???? ???? ??? ??? ???? ???? ??? ?????. ? ??? String ??? ???? ???? str? ?? ???? ???? ???? ????. ? ?? ??? ??? ??? ???? ???? JavaScript??? ??? ??? ??? ?????.
?? ???? ???? ???? ?????.
alert( typeof str_object ); // ?? object
alert( typeof str1 ); // ?? ???
alert( typeof str2 ); ?? ??
?? ???????????????????????????????????????????????????????????????????????????????? ????? ??? ?? ? ??? ??
?? ???? ?? ?? ??? ???? ?????. ???? ??:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</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>String? indexOf()? ???? ? ?? ??? ????. ????? ??? ??:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</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>
match() ??? ??? ?? ? ?????. ????? ?? ??? ??, ???? ? ??? ?????.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</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>
replace() ???? ???? ?? ??? ?? ??? ????.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <button onclick="myFunction()">點我</button> <p id="demo">請訪問 Microsoft!</p> <script> function myFunction() { var str = document.getElementById("demo").innerHTML; var txt = str.replace("Microsoft","php.cn"); document.getElementById("demo").innerHTML = txt; } </script> </body> </html>
??? ?/??? ???? toUpperCase() / toLowerCase() ??? ?????.
var txt="Hello World!"; // Stringvar txt1=txt.toUpperCase(); ????
var txt2=txt.toLowerCase(); // txt2 ???? ???? ?????.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</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[2]; } </script> </body> </html>?? ?? Javascript( ) ??????, ???, ?? ?? ?? ? ?? ??? ?????. ?? JavaScript ?? ??:
var txt="??? ?? ???? ? "???"???.";document.write(txt); JavaScript??? ?????? ????? ???? ???? ???? ?????. ?? ?? ???? ??? ?? ???? ?????. We are the ?? ?? ??? ????? ????? ???? ???? ????? ??? ? ????. var txt="We are the ?? "Vikings " from the north."; JavaScript? ??? ??? ???? ?????. We are the ?? "Vikings" from the north. ?? ??? ?? ?? ??, ?? ??? ???? ????. ????? ???? ?????? ? ????. Output " ????
document.write(txt);
' ?????