JavaScript 字串(String) 對象

JavaScript 字串(String) 物件

String 物件用於處理現有的字元區塊。

如何使用長度屬性來計算字串的長度:

<html>
<body>
<script type="text/javascript">
var txt="Hello World!"
document.write(txt.length)
</script>
</body>
</html>

如何為字串新增樣式:

<html>
<meta charset="utf-8">
<body>
<script type="text/javascript">
var txt="Hello World!"
document.write("<p>Big: " + txt.big() + "</p>")
document.write("<p>Small: " + txt.small() + "</p>")
document.write("<p>Bold: " + txt.bold() + "</p>")
document.write("<p>Italic: " + txt.italics() + "</p>")
document.write("<p>Blink: " + txt.blink() + " (does not work in IE)</p>")
document.write("<p>Fixed: " + txt.fixed() + "</p>")
document.write("<p>Strike: " + txt.strike() + "</p>")
document.write("<p>Fontcolor: " + txt.fontcolor("Red") + "</p>")
document.write("<p>Fontsize: " + txt.fontsize(16) + "</p>")
document.write("<p>Lowercase: " + txt.toLowerCase() + "</p>")
document.write("<p>Uppercase: " + txt.toUpperCase() + "</p>")
document.write("<p>Subscript: " + txt.sub() + "</p>")
document.write("<p>Superscript: " + txt.sup() + "</p>")
</script>
</body>
</html>

如何使用indexOf() 來定位字串中某一個指定的字元首次出現的位置:

<html>
<meta charset="utf-8">
<body>
<script type="text/javascript">
var str="Hello world!"
document.write(str.indexOf("Hello") + "<br />")
document.write(str.indexOf("World") + "<br />")
document.write(str.indexOf("world"))
</script>
</body>
</html>

如何使用match() 來查找字串中特定的字符,並且如果找到的話,則返回這個字元:

<html>
<meta charset="utf-8">
<body>
<script type="text/javascript">
var str="Hello world!"
document.write(str.match("world") + "<br />")
document.write(str.match("World") + "<br />")
document.write(str.match("worlld") + "<br />")
document.write(str.match("world!"))
</script>
</body>
</html>

如何使用replace( ) 方法在字串中用某些字元取代另一些字元:

<html>
<meta charset="utf-8">
<body>
<script type="text/javascript">
var str="Visit Microsoft!"
document.write(str.replace(/Microsoft/,"PHP中文网"))
</script>
</body>
</html>

字串物件

#字串物件用於處理已有的字元區塊。

範例:

下面的範例使用字串物件的長度屬性來計算字串的長度。

var txt="Hello world!"
document.write(txt.length)

上面的程式碼輸出為:

##12


下面的範例使用字串物件的toUpperCase()方法將字串轉換為大寫:

var txt="Hello world!"

document.write(txt.toUpperCase( ))

上面的程式碼輸出為:

HELLO WORLD!


字串使用strong>split()函數轉為陣列:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(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[0];
}
</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.

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

代碼  

輸出

\'    單引號   

#\"    雙引號   

##\\     斜桿   

# r    回車   

\t    tab    

\b    空格   

\f    換頁   


##字串屬性與方法

屬性:

#字串屬性與方法

屬性:

length

prototype

constructor

方法:

charAt()

charCodeAt()

#concat()

fromCharCode()

indexOf()

lastIndexOf()

match()

replace()

search()

slice()

split()

substr()

substring()

toLowerCase()

toUpperCase()


#valueOf()################
繼續學習
||
<html> <meta charset="utf-8"> <body> <script type="text/javascript"> var str="Hello world!" document.write(str.indexOf("Hello") + "<br />") document.write(str.indexOf("World") + "<br />") document.write(str.indexOf("world")) </script> </body> </html>
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!