ylbtech-string:字符串(string)對象 |
JS:1.8.0,JavaScript String(字符串)對象 實例返回頂部 |
<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>
<html>
<body>
<h3>replace(要替換內容,替換成的內容)</h3>
<script language="javascript">
var txt="某某喜歡王帥!";
document.write("替換前的內容:"+txt+"<br>");
document.write("替換后的內容:"+txt.replace("某某","鳳姐"));
</script>
</body>
</html>
<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>