JS:1.8,字符串(string)對象(length,indexOf(),lastIndexOf(),replace(),match(),toUpperCase(),toLowerCase())


ylbtech-string:字符串(string)對象
JS:1.8.0,JavaScript String(字符串)對象 實例返回頂部
計算字符串的長度
如何使用長度屬性來計算字符串的長度。
為字符串添加樣式
如何為字符串添加樣式。
indexOf() 方法
如何使用 indexOf() 來定位字符串中某一個指定的字符首次出現的位置。
match() 方法
如何使用 match() 來查找字符串中特定的字符,並且如果找到的話,則返回這個字符。
如何替換字符串中的字符 - replace()
如何使用 replace() 方法在字符串中用某些字符替換另一些字符。
JS:1.8.1,length屬性 返回頂部
<html>
<body>

<script type="text/javascript">

var txt="Hello World!"

//輸出字符串的長度
document.write(txt.length)

</script>

</body>
</html>
JS:1.8.2,indexOf(),lastIndexOf() 返回頂部
<html>
<body>
<script language="javascript">
var txt="Hello world!";

// indexOf(),lastIndexOf() 來定位字符串中某一個指定的字符首次出現的位置
//若不存在則返回“-1”

document.write(txt+"<br>");

document.write("該字符串的長度為:"+txt.length+" <br>");
document.write("從前往后找,第一位字符'l'它的索引位置為:"+txt.indexOf('l'+"<br>"));
document.write("從后往前查,第一個字符'l'它的索引位置為:"+txt.lastIndexOf('l')+"<br>");
</script>

</body>
</html>
JS:1.8.3,replace()返回頂部
<html>
<body>

<h3>replace(要替換內容,替換成的內容)</h3>
<script language="javascript">
var txt="某某喜歡王帥!";
document.write("替換前的內容:"+txt+"<br>");
document.write("替換后的內容:"+txt.replace("某某","鳳姐"));
</script>
</body>
</html>
JS:1.8.4,match() 返回頂部
<html>
<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>
JS:1.8.5,toUpperCase(),toLowCase() 返回頂部
<html>
<body>
<script language="javascript">
var txt="Hello world!";

document.write(txt+"<br>");
document.write("把所有字符轉為大寫:"+txt.toUpperCase()+"<br>");
document.write("把所有字符轉為小寫:"+txt.toLowerCase());
</script>

</body>
</html>

 

warn 作者:ylbtech
出處:http://ylbtech.cnblogs.com/
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM