亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

JavaScript 字串(String)對象

定義字串(String)物件

JavaScript String 物件用於處理文字字串。建立 String 物件語法如下:

<script language="JavaScript">
var str_object = new String( str );
var str1 = String( str );
var str2 = str;
</script>

以上三種方法中,只有第一種是使用 String 建構函數(shù)嚴格的定義一個字串對象,傳回的也是一個物件(object)。第二種是呼叫 String 函數(shù),將轉換參數(shù) str 為原始字串字串並傳回。第三種是定義一個字串變量,但在 JavaScript 仍然按照字串物件來處理。

執(zhí)行下面的語句便可知道它們的差異:

alert( typeof str_object ); // 輸出object
alert( typeof str1 ); // 輸出string
alert ( typeof str2 ); // 輸出string

String 物件屬性


##屬性? ? ? ? ? ? ? ?

描述






#######################################################################################”。 ###############constructor ? ?對建立該物件的函數(shù)的引用? ?######length ? ?字串的長度? ?######prototype ? ?向物件新增屬性與方法? ?######prototype ? ?新增屬性與方法? ?## #############字串(String)使用長度屬性length來計算字串的長度:###
<!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>
###字串使用indexOf() 來定位字串中某一個指定的字符首次出現(xiàn)的位置:###
<!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()函數(shù)用來查找字串中特定的字符,並且如果找到的話,則返回這個字符。 ###
<!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>
###字串大小寫轉換使用函數(shù)?toUpperCase()?/?toLowerCase():#########var txt="Hello World!";?????? // String###var txt1=txt .toUpperCase();?? // txt1 文字會轉換成大寫###var txt2=txt.toLowerCase();?? // txt2 文字會轉換為小寫#########字串使用strong>split()函數(shù)轉為陣列:###
<!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="We are the so-called "Vikings" from the north.";###document.write(txt);# #####

在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.

下表列出其他特殊字符,可以使用反斜線轉義特殊字符:

代碼? ?#輸出

\' ? ?單引號? ?

#\' ? ?單引號? ?

\" ? ?雙引號? ?

#\\ ? ?斜桿#? ?##?

?##\n ? ?更換

##

繼續(xù)學習
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <script type="text/javascript"> var str = "ipnx.cn"; document.write( str.split(".") + "<br />" ); document.write( str.split("") + "<br />" ); document.write(str.split(".", 2)); </script> </head> <body> </body> </html>