標籤:style http java 使用 width javascript
String的常用方法
方法 |
說明 |
Anchor() |
建立html錨 |
Concat() |
把字串串連起來 |
indexOf() |
尋找字元出現的位置 |
lastIndexOf() |
尋找最後出現字元 |
charAt() |
返回指定位置的字元 |
Substring() |
截取字串 |
Substr() |
截取字串 |
Spilt() |
分割字串 |
toLowerCase() |
把字串轉換成小寫字母 |
toUpperCase() |
把字串轉換成大寫字母 |
Sub() |
把字串顯示為下標 |
Sup() |
把字串顯示為上標 |
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
<script type="text/javascript" language="javascript">
/************建立字串對象***********/
//建立字串對象(方法一)
//var str = "hello world";
//建立字串對象(方法一)
//var str = new String("hello world");
/***********字串對象的常用方法***************/
var str = "hello world";
//indexOf用來尋找指定子字串的第一出現的索引位置,找不到就返回-1
//document.write(str.indexOf("o",0));//輸出4
//document.write(str.indexOf("o",5));//輸出7
//lastIndexOf用來尋找指定子字串出現的最後位置,找不到就返回-1(注意:本方法是從字串的最後向前面進行尋找,第二個參數代表開始尋找的索引位置)
//document.write(str.lastIndexOf(‘o‘,str.length));
/*********substring與substr*********************/
var str = "hello world";
//參數一:開始截取的下標 參數二:截取到第幾個字元
document.write(str.substring(1,3));//輸出el
document.write("<br>");
//參數一:開始截取的下標 參數二:截取多少個字元
document.write(str.substr(1,3));
document.write("<br>");
document.write(str.length);//輸出字串的長度
</script>
</head>
<body>
</body>
</html>