3-3 字串

JavaScript rƫAiHΨӪܤ@CreCڭ̥unrAb۲ŪAA̡ANiHΦ@ӦrCAiH]tbAbAr̡AӳA]iH]tbAbAr̡CUCOrꪺdҡG

"This is a string" 'This is a string quoted by single quotes' "This is a string with 'single' quotes" 'This is a string with "double" quotes' "This another string with \"double\" quotes"
Ъ`NbWzdҤ̫@Ӧr]tF޸AFױ޸ӥγ~]Хܦrꪺ}lM^Aڭ̭nb޸e[Wϱ׽u]\^C

JavaScript rꪫƤF\hkAoǤkirꥻiUحקέpAҳyĪGiCXpUG

Example]string01.htm^G

WzdҪlɮצpUG

lɡ]string01.htm^G]ǦϰUYi^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>rꪫ󪺤kPʽ</h2>
<hr>

<script>
myStr = "Tang Poem: ho`LAıeC";
document.write("rGmyStr = " + myStr + "<br>");
document.write("rסGmyStr.length = " + myStr.length + "<br>");
document.write("WjrGmyStr.big() = " + myStr.big() + "<br>");
document.write("prGmyStr.small() = " + myStr.small() + "<br>");
document.write("{{rGmyStr.blink() = " + myStr.blink() + "<br>");
document.write("ܶGmyStr.bold() = " + myStr.bold() + "<br>");
document.write("ܱGmyStr.italics() = " + myStr.italics() + "<br>");
document.write("ܵerGmyStr.fixed() = " + myStr.fixed() + "<br>");
document.write("brGmyStr.strike() = " + myStr.strike() + "<br>");
document.write("ܤUСGmyStr.sub() = " + myStr.sub() + "<br>");
document.write("ܤWСGmyStr.sup() = " + myStr.sup() + "<br>");
document.write("]wCGmyStr.fontcolor(\"salmon\") = " + myStr.fontcolor("salmon") + "<br>");
document.write("]wrjpGmyStr.fontsize(5) = " + myStr.fontsize(5) + "<br>");
document.write("jgrGmyStr.toUpperCase() = " + myStr.toUpperCase() + "<br>");
document.write("pgrGmyStr.toLowerCase() = " + myStr.toLowerCase() + "<br>");
document.write("rñơGmyStr.concat(\"s[\") = " + myStr.concat("s[") + "<br>");
document.write("XrGmyStr.charAt(13) = " + myStr.charAt(13) + "<br>");
document.write("X Unicode rGmyStr.charCodeAt(13) = " + myStr.charCodeAt(13) + "<br>");
document.write("XlrGmyStr.substr(13, 5) = " + myStr.substr(13, 5) + "<br>");
document.write("XlrGmyStr.substring(13, 15) = " + myStr.substring(13, 15) + "<br>");
document.write("MlrGmyStr.indexOf(\"\") = " + myStr.indexOf("") + "<br>");
document.write("MlrGmyStr.lastIndexOf(\"\") = " + myStr.lastIndexOf("") + "<br>");
</script>

<hr>
</body>
</html>

n`NOAb檫󪺤kɡAJ޼ƥiiLAA@wnAnDM JavaScript ƬO@PCrꪫ`ΪkiHCpUG

ʽΤkĪHTML
StringlengthǦ^rꪺ
big()Wjrꪺr<big>...</big>
small()prꪺr<small>...</small>
blink(){{r]kuAΩ Netscape sAAΩ IE s^<blink>...</blink>
bold()ܶ<b>...</b>
italics()ܱ<i>...</i>
fixed()ܵer<tt>...</tt>
strike()br<strike>...</strike>
sub()ܤU<sub>...</sub>
sup()ܤW<sup>...</sup>
fontcolor()]wrꪺC<font color=...>...</font>
fontsize()]wrꪺrjp<font size=...>...</font>
toUpperCase()jgr
toLowerCase()pgr
concat()rñơ]ĩϥΥ[^
charAt(n)X n Ӧr]n=0 NĤ@Ӧr^
charCodeAt(n)X n Ӧr]n=0 NĤ@Ӧr^Aഫ Unicode
substr(m, n)Ǧ^@ӦrAqm m }lAB׬ n
substring(m, n)Ǧ^@ӦrAqm m }lAm n-1
indexOf(str)Mlr str brꪺĤ@X{m
lastIndexOf(str)Mlr str brꪺ̫@X{m

Hint
ЯSO`N substr(m, n) M substring(m, n) b\WtCpG text = "@OdUe"Ah
  • text.substr(3,5)|Ǧ^ "dUe"]3Ӧr}lA5Ӧr^
  • text.substring(3,5)|Ǧ^ "dU"]3Ӧr}lA4Ӧr^

bWArꪺAuF indexOf() M lastIndexOf() ӤkAƹW JavaScript rꪺMN\hjj\AҦp searchBmatchBreplace ơAoǥ\κ١uqΪܪkvAN|b᳹`JӤСC

ɭԧڭ̤]iHNrݦO JavaScript OӰ椧AoɭԩҥΨ쪺OO eval()AOSOAΩuϥΰjгyܼơvAШUCdҡG

Example]eval01.htm^G

WzdҪlɮצpUG

lɡ]eval01.htm^G]ǦϰUYi^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>pϥ eval()</h2>
<hr>

<script>
for (i=1; i<=3; i++){
	command = "x" + i + "=" + i*i*i;
	eval(command);
}
document.write("x1 = " + x1 + "<br>");
document.write("x2 = " + x2 + "<br>");
document.write("x3 = " + x3 + "<br>");
</script>

<hr>
</body>
</html>

bWzdҤAڭ̧ JavaScript n檺RObrܼ commandAMA eval O "" oӦrANiHгyXTܼ x1Bx2 M x3C

JavaScript ٦@ǤةΨϥΪ̦۳ЪAoǪAڭ̥iHϥ toString() ഫrܡAҦpG

Example]toString01.htm^G

WzdҪlɮצpUG

lɡ]toString01.htm^G]ǦϰUYi^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>pϥ toString()</h2>
<hr>

<script>
x = "This is a string";
document.write("rG" + x.toString()+"<br>");
x = new Date();
document.write("G" + x.toString()+"<br>");
x = ["Mon", "Tue", "Wed"];
document.write("}CG" + x.toString()+"<br>");
function square(n){
	return(n*n);
}
document.write("ơG" + square.toString()+"<br>");
student = new Object();
student.name = "Timmy";
student.age = "25";
student.phone = "575-1114";
document.write("ۭqG" + student.toString()+"<br>");
</script>

<hr>
</body>
</html>

ѤWҥiHݥXAtoString() 欰󫬺AөwAiCpUG

toString() G
Array]}C^N Array ഫrAΦHrs_ӪGAG P Array.toString() M Array.join() o쪺GۦP
Boolean]L^pGLȬ TrueA|Ǧ^ "true"F_h|Ǧ^ "false"
Date]^Ǧ^ܤrΦ
Error]~^Ǧ^]t~Tr
Function]ơ^Ǧ^ƪwq
Number]Ʀr^Ǧ^Ʀrrܪk
String]r^Ǧ^ String 󪺭
ۭqǦ^ "[object Object]"

~AYnNrনƭȡAi parseInt() άO parseFloat() oӨơApUG

dҥie@p`C
JavaScript {]pPΡGΩΤ