JavaScript学习与实践(19)
String 对象,被用于操作存储的一小段文本,
管用手法 下面是一些例子:
1,返回一个字符串的长度:(怎么用length属性来返回字符串的长度)
<html> <body> <script type="text/javascript"> var txt="Hello World!" document.write(txt.length) </script> </body> </html>
2,字符串的样式风格
.
<html>
<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>")
document.write("<p>Link: " + txt.link("http://www.w3schools.com") + "</p>")
</script>
</body>
</html>
大家可以自己去看一下结果,
下面是一个方法
indexOf(),这个方法是从一个字符串中找出指定字符串第一次出现的位置。
下面是一个例子,大家可以拷贝到自己的桌面去运行
<html> <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>
运行的结果是:
0
-1
6
另外一个方法是 match(),他用于在一个字符串中查找一个特定的字符串,要是能找到就返回这个字符串,如果找不到就返回NULL
例子:
<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>
下面一个方法是replace(),他用于用特定的字符串替换一个字符串中指定的内容,这个方法一般很常用,下面给出一个简单的例子
<html> <body> <script type="text/javascript"> var str="Visit Microsoft!" document.write(str.replace(/Microsoft/,"W3Schools")) </script> </body> </html>
下面是STRING的一些方法和属性的介绍:
FF: Firefox, N: Netscape, IE: Internet Explorer
Method | Description | FF | N | IE |
---|---|---|---|---|
anchor() | Creates an HTML anchor | 1 | 2 | 3 |
big() | Displays a string in a big font | 1 | 2 | 3 |
blink() | Displays a blinking string | 1 | 2 | |
bold() | Displays a string in bold | 1 | 2 | 3 |
charAt() | Returns the character at a specified position | 1 | 2 | 3 |
charCodeAt() | Returns the Unicode of the character at a specified position | 1 | 4 | 4 |
concat() | Joins two or more strings | 1 | 4 | 4 |
fixed() | Displays a string as teletype text | 1 | 2 | 3 |
fontcolor() | Displays a string in a specified color | 1 | 2 | 3 |
fontsize() | Displays a string in a specified size | 1 | 2 | 3 |
fromCharCode() | Takes the specified Unicode values and returns a string | 1 | 4 | 4 |
indexOf() | Returns the position of the first occurrence of a specified string value in a string | 1 | 2 | 3 |
italics() | Displays a string in italic | 1 | 2 | 3 |
lastIndexOf() | Returns the position of the last occurrence of a specified string value, searching backwards from the specified position in a string | 1 | 2 | 3 |
link() | Displays a string as a hyperlink | 1 | 2 | 3 |
match() | Searches for a specified value in a string | 1 | 4 | 4 |
replace() | Replaces some characters with some other characters in a string | 1 | 4 | 4 |
search() | Searches a string for a specified value | 1 | 4 | 4 |
slice() | Extracts a part of a string and returns the extracted part in a new string | 1 | 4 | 4 |
small() | Displays a string in a small font | 1 | 2 | 3 |
split() | Splits a string into an array of strings | 1 | 4 | 4 |
strike() | Displays a string with a strikethrough | 1 | 2 | 3 |
sub() | Displays a string as subscript | 1 | 2 | 3 |
substr() | Extracts a specified number of characters in a string, from a start index | 1 | 4 | 4 |
substring() | Extracts the characters in a string between two specified indices | 1 | 2 | 3 |
sup() | Displays a string as superscript | 1 | 2 | 3 |
toLowerCase() | Displays a string in lowercase letters | 1 | 2 | 3 |
toUpperCase() | Displays a string in uppercase letters | 1 | 2 | 3 |
toSource() | Represents the source code of an object | 1 | 4 | - |
valueOf() | Returns the primitive value of a String object | 1 | 2 | 4 |
String Object Properties
Property | Description | FF | N | IE |
---|---|---|---|---|
constructor | A reference to the function that created the object | 1 | 4 | 4 |
length | Returns the number of characters in a string | 1 | 2 | 3 |
prototype | Allows you to add properties and methods to the object | 1 | 2 | 4 |