近來對JS方面的東西涉及的太多了,要學習它了,應該容易入門,因爲它也是OOP的語言,筆者將運用大量的筆記的形式來記錄學習的軌跡:
String字符串常用的幾個實際的例子:
1.求長度:直接用String.lengh就可以了;
2.樣式:
<html>
<body>
<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>Small: " + txt.small() + "</p>")
document.write("<p>Bold: " + txt.bold() + "</p>")
document.write("<p>Italic: " + txt.italics() + "</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>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>Fontsize: " + txt.fontsize(16) + "</p>")
document.write("<p>Lowercase: " + txt.toLowerCase() + "</p>")
document.write("<p>Uppercase: " + txt.toUpperCase() + "</p>")
document.write("<p>Uppercase: " + txt.toUpperCase() + "</p>")
document.write("<p>Subscript: " + txt.sub() + "</p>")
document.write("<p>Superscript: " + txt.sup() + "</p>")
document.write("<p>Superscript: " + txt.sup() + "</p>")
document.write("<p>Link: " + txt.link("[url]http://www.w3school.com.cn[/url]") + "</p>")
</script>
</script>
</body>
</html>
</html>
3.運用indexOf來定位字符串中某指定的字符首次出現的位置:
<html>
<body>
<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"))
document.write(str.indexOf("Hello") + "<br />")
document.write(str.indexOf("World") + "<br />")
document.write(str.indexOf("world"))
</script>
</body>
</html>
</html>
4.運用match來查找某指定的字符,如果找到則返回,沒有的話就返回null:
<html>
<body>
<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!"))
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>
</html>
</body>
</html>
</html>
5.還有replace方法,主要是匹配正則表達式來使用;
字符串是 JavaScript 的一種基本的數據類型。
String 對象的 length 屬性聲明瞭該字符串中的字符數。String 類定義了大量操作字符串的方法。
需要注意的是,JavaScript 的字符串是不可變的,String 類定義的方法都不能改變字符串的內容。
String 對象的方法
FF: Firefox, N: Netscape, IE: Internet Explorer
方法 | 描述 | FF | N | IE |
---|---|---|---|---|
anchor() | 創建 HTML 錨。 | 1 | 2 | 3 |
big() | 用大號字體顯示字符串。 | 1 | 2 | 3 |
blink() | 顯示閃動字符串。 | 1 | 2 | |
bold() | 使用粗體顯示字符串。 | 1 | 2 | 3 |
charAt() | 返回在指定位置的字符。 | 1 | 2 | 3 |
charCodeAt() | 返回在指定的位置的字符的 Unicode 編碼。 | 1 | 4 | 4 |
concat() | 連接字符串。 | 1 | 4 | 4 |
fixed() | 以打字機文本顯示字符串。 | 1 | 2 | 3 |
fontcolor() | 使用指定的顏色來顯示字符串。 | 1 | 2 | 3 |
fontsize() | 使用指定的尺寸來顯示字符串。 | 1 | 2 | 3 |
fromCharCode() | 從字符編碼創建一個字符串。 | 1 | 4 | 4 |
indexOf() | 檢索字符串。 | 1 | 2 | 3 |
italics() | 使用斜體顯示字符串。 | 1 | 2 | 3 |
lastIndexOf() | 從後向前搜索字符串。 | 1 | 2 | 3 |
link() | 將字符串顯示爲鏈接。 | 1 | 2 | 3 |
localeCompare() | 用本地特定的順序來比較兩個字符串。 | 1 | 4 | 4 |
match() | 找到一個或多個正在表達式的匹配。 | 1 | 4 | 4 |
replace() | 替換與正則表達式匹配的子串。 | 1 | 4 | 4 |
search() | 檢索與正則表達式相匹配的值。 | 1 | 4 | 4 |
slice() | 提取字符串的片斷,並在新的字符串中返回被提取的部分。 | 1 | 4 | 4 |
small() | 使用小字號來顯示字符串。 | 1 | 2 | 3 |
split() | 把字符串分割爲字符串數組。 | 1 | 4 | 4 |
strike() | 使用刪除線來顯示字符串。 | 1 | 2 | 3 |
sub() | 把字符串顯示爲下標。 | 1 | 2 | 3 |
substr() | 從起始索引號提取字符串中指定數目的字符。 | 1 | 4 | 4 |
substring() | 提取字符串中兩個指定的索引號之間的字符。 | 1 | 2 | 3 |
sup() | 把字符串顯示爲上標。 | 1 | 2 | 3 |
toLocaleLowerCase() | 把字符串轉換爲小寫。 | - | - | - |
toLocaleUpperCase() | 把字符串轉換爲大寫。 | - | - | - |
toLowerCase() | 把字符串轉換爲小寫。 | 1 | 2 | 3 |
toUpperCase() | 把字符串轉換爲大寫。 | 1 | 2 | 3 |
toSource() | 代表對象的源代碼。 | 1 | 4 | - |
toString() | 返回字符串。 | - | - | - |
valueOf() | 返回某個字符串對象的原始值。 | 1 | 2 | 4 |
String 對象的屬性
FF: Firefox, N: Netscape, IE: Internet Explorer
屬性 | 描述 | FF | N | IE |
---|---|---|---|---|
constructor | 對創建該對象的函數的引用 | 1 | 4 | 4 |
length | 字符串的長度 | 1 | 2 | 3 |
prototype | 允許您向對象添加屬性和方法 | 1 | 2 | 4 |