var guitar_string = new String("G" );
var guitar_string = "G";
<script language="JavaScript">
<!--
var guitar_string = new String("G" );
window.alert(guitar_string.constructor);
//-->
</script>
<html>
<head>
<script language="JavaScript">
<!--
var myname = new String("John");
//-->
</script>
</head>
<body>
<script language="JavaScript">
<!--
document.write("The name has " + myname.length + " characters.");
//-->
</script>
</body>
</html>
<script language="JavaScript">
<!--
var the_text = "Character";
window.alert( "The last character of " + the_text + " is " + the_text.charAt(the_text.length-1) );
//-->
</script>
<SCRIPT language="JavaScript">
<!--
String.prototype.attitude = "cool";
var rightnow = new String("Joe");
window.alert("This place is " + rightnow.attitude);
//-->
</script>
<body>
<script language="JavaScript">
<!--
var the_text = "Character";
window.alert( "The first character of " + the_text + " is " + the_text.charAt(0) );
//-->
</script>
</body>
<script language="JavaScript">
<!--
var the_text = "Character";
window.alert( "The first character of " + the_text + " is?q&#" + the_text.charCodeAt(0) + ";");
//-->
</script>
<script language="JavaScript">
<!--
var string1 = "太陽";
var string2 = "星星";
var string3 = "月亮";
window.alert( string1.concat(string2, string3) );
window.alert( string3.concat(string2, string1) );
//-->
</script>
window.alert( String.fromCharCode(72,73) );
<script language="JavaScript">
<!--
var the_text = "Cool";
var position = the_text.indexOf("C");
window.alert( "Your character is at position " + position );
//-->
</script>
<script language="JavaScript">
<!--
var the_text = "Cool";
var position = the_text.indexOf("C");
if ( position == -1)
window.alert( "Your character is not in the string! " );
else
window.alert( "Your character is at position " + position );
//-->
</script>
<script language="JavaScript">
<!--
var the_text = "Cool";
var position = the_text.lastIndexOf("o");
if ( position == -1)
window.alert( "Your character is not in the string! " );
else
window.alert( "Your character is at position " + position );
//-->
</script>
<script>
var str="Hello world!";
document.write(str.match("world") + "<br>");
document.write(str.match("World") + "<br>");
document.write(str.match("world!"));
</script>
<body>
<p>替換 "Microsoft" 為 "W3cSchool" :</p>
<button onclick="myFunction()">點我
<p id="demo">請訪問 Microsoft!
<script>
function myFunction() {
var str = document.getElementById("demo").innerHTML;
var txt = str.replace("Microsoft","W3cSchool");
document.getElementById("demo").innerHTML = txt;
}
</script>
如果沒有找到任何匹配的子串,則返回-1。
<body>
<p id="demo">單擊顯示查找的位置</p>
<button onclick="myFunction()">點我</button>
<script>
function myFunction(){
var str="Visit W3CSchool!";
var n=str.search("W3CSchool");
document.getElementById("demo").innerHTML=n;
}
</script>
</body>
<script language="JavaScript">
<!--
var the_text = "Do not cut this short!";
var shorter_string = the_text.slice(0,7);
window.alert(shorter_string);
//-->
</script>
<body>
<H2>Fruit to Eat Today:</H2>
<script language="JavaScript">
<!--
var the_text = "orange:apple:pear:grape";
var split_text = the_text.split(":");
var end_count = split_text.length;
for (count=0; count<end_count; count+=1)
{
document.write( split_text[count] + "<br>" );
}
//-->
</script>
</body>
string.substr( start , length )
| 參數 | 說明 |
|---|---|
| start | 必需。要抽取的子串的參數 start,必須是數值。如果是負數,那麼該參數聲明從字串的尾部開始算起的位置。 也就是說,-1 指字串中最後一個字元,-2 指倒數第二個字元,以此類推。 |
| length | 可選。一個非負的整數,要提取的子字串的長度。 如果省略該參數,那么返回的子字串會一直到字串的結尾。 |
下例從字串從 0 開始, 取出 8 個字元。
<script language="JavaScript">
<!--
var the_text = "Hello world!";
var shorter_string = the_text.substr(0,8);
window.alert(shorter_string);
//-->
</script>
string.substring(from, to)
| 參數 | 說明 |
|---|---|
| from | 必需。一個非負的整數,規定要提取的子串的第一個字元在 string Object 中的位置。 |
| to | 可選。一個非負的整數,比要提取的子串的最後一個字元在 string Object 中的位置多 1。 如果省略該參數,那么返回的子字串會一直到字串的結尾。 |
下例從字串取出 0 到 8 的字元。
<script language="JavaScript">
<!--
var the_text = "Hello world!";
var shorter_string = the_text.substring(0,9);
window.alert(shorter_string);
//-->
</script>
Javascript的基本類型有五种,分別是Number、String、Undefined、null和Boolean; 內置物件有Array、Boolean、Object、Function、Number、String等。
<p id="demo"></p>
<script>
const fruits = ["Banana", "Orange", "Apple", "Mango"];
let text = fruits.toString();
document.getElementById("demo").innerHTML = text;
</script>
<script language="JavaScript">
<!--
work_car = {seats:"cloth", engine:"V-6", theradio:"Tape Deck" };
document.write( work_car.toString() );
//-->
</script>
"function functionname() { [native code] }”
例如
function func() {
console.log("I am a function");
}
func.toString();
輸出:
"function func() {
console.log("I am a function");
}"
number.toString(radix)
| 參數 | 說明 |
|---|---|
| radix | 可選。規定表示數字的基數,使 2 ~ 36 之間的整數。若省略該參數,?使用基數10。但是要注意,如果該參數是 10 以外的其他值,則ECMAScript 標准允許實現返回任意值。
|
<body>
<script>
function myFunction() {
var num = 15;
var a = num.toString();
var b = num.toString(2);
var c = num.toString(8);
var d = num.toString(16);
var n = a + "<br>" + b + "<br>" + c + "<br>" + d;
document.getElementById("demo").innerHTML=n;
}
</script>
<p>Click the button to display the formatted numbers.
<p><button onclick="myFunction()">點我</button>
<p id="demo"></p>
</body>
<script language="JavaScript">
<!--
var the_text = "Do not cut this short!";
window.alert( the_text.toLowerCase() );
//-->
</script>
<script language="JavaScript">
<!--
var the_text = "Do not cut this short!";
window.alert( the_text.toUpperCase() );
//-->
</script>
<body>
<script language="JavaScript">
<!--
var the_text = "處理字串";
var text_anchor = the_text.anchor("top");
document.write(text_anchor);
//-->
</script>
</body>
將會輸出下面的 HTML:
<a name="top">處理字串</a>
<body>
<script language="JavaScript">
<!--
var the_text = "大型文字";
document.write( the_text + "<br>" + the_text.big() );
//-->
</script>
</body>
<body>
<script language="JavaScript">
<!--
var the_text = "閃動文字";
document.write( the_text + "<br>" + the_text.blink() );
//-->
</script>
</body>
<body>
<script language="JavaScript">
<!--
var the_text = "加粗文字";
document.write( the_text + "<br>" + the_text.bold() );
//-->
</script>
</body>
<body>
<script language="JavaScript">
<!--
var the_text = "I am typewriter text, on a computer!";
document.write( the_text.fixed() );
//-->
</script>
</body>
<body>
<script language="JavaScript">
<!--
var the_text = "姓名";
document.write( "請先確定" + the_text.fontcolor("red") );
//-->
</script>
</body>
<body>
<script language="JavaScript">
<!--
var the_text = "請先確定姓名";
document.write( the_text.fontsize(5) );
//-->
</script>
</body>
<body>
<SCRIPT language="JavaScript">
<!--
var the_text = "斜體文字";
document.write( the_text + "<br>" + the_text.italic() );
//-->
</SCRIPT>
</body>
<body>
<script language="JavaScript">
<!--
var the_text = "Javascript Programming";
var text_link = the_text.link("http://www.csie.ntu.edu.tw/~sylee/courses/webprog/index.html");
document.write(text_link);
//-->
</script>
</body>
下列程式建立一個指到網頁某處的錨點, 並由點選鏈結跳到該處。
<body>
<script language="JavaScript">
<!--
var the_text = "處理字串";
var text_anchor = the_text.anchor("top");
document.write("<H2>" + text_anchor +"</H2>");
//-->
</script>
<P>Some text in body.
<script language="JavaScript">
<!--
var the_text = "回到啟始點";
var text_link = the_text.link("#top");
document.write(text_link);
//-->
</script>
</body>
<body>
<script language="JavaScript">
<!--
var the_text = "小型文字";
document.write( the_text + "<br>" + the_text.small() );
//-->
</script>
</body>
<body>
<script language="JavaScript">
<!--
var the_text = "I shouldn't wear stripes!";
document.write( the_text + "<br>" + the_text.strike() );
//-->
</script>
</body>
<body>
<script language="JavaScript">
<!--
var sub_text = "2";
document.write( "Water is H" + sub_text.sub() + "O");
//-->
</script>
</body>
<body>
<script language="JavaScript">
<!--
var sup_text = "2";
document.write( "2 " + sup_text.sup() + " is equal to 4.");
//-->
</script>
</body>
<html>
<head>
<script language="JavaScript">
<!--
function get_name()
{
var the_text=window.prompt("Enter your first and last name.","");
if(the_text.indexOf(" ") == -1) //不是有名有姓
{
window.alert("Put a space between your first and last name! Try again.");
get_name();
}
var split_text = the_text.split(" "); //姓和名分開
if ((split_text[0].charAt(0)!="Z") && (split_text[0].charAt(0)!="z")) //名字不是Z或z起頭
{
var shorter_fn_string = split_text[0].substring(1,split_text[0].length) ;
new_fn_string = "W" + shorter_fn_string; //名字改成W起頭
}
else
{
var shorter_fn_string = split_text[0].substring(1,split_text[0].length) ;
new_fn_string = "Z" + shorter_fn_string; //否則名字改成Z起頭
}
if ((split_text[1].charAt(0)!="Z")&&(split_text[1].charAt(0)!="z")) //姓不是Z或z起頭
{
var shorter_ln_string = split_text[1].substring(1,split_text[1].length) ;
new_ln_string = "W" + shorter_ln_string; //姓改成W起頭
}
else
{
var shorter_ln_string = split_text[1].substring(1,split_text[1].length) ;
new_ln_string = "Z" + shorter_ln_string; //否則姓改成Z起頭
}
document.write( "<H1>Welcome! <p>Now your name is <font color='blue'>Mr. "
+ new_fn_string + " " + new_ln_string + "!</font></H1>");
}
//-->
</script>
</head>
<body>
<script language="JavaScript">
<!--
get_name();
//-->
</script>
</body>
</html>