String对象知识点总结
知识点一:
String对象和其他类型的字符串没有实际的不同。
var book_name = new String("My Name is Tony");
var book_name = "My Name is Tony";
知识点二:
String对象提供的唯一独特的属性是prototype,它能用于增加自定义方法到String对象。
知识点三:
String.indexOf(Substring,Start) 是搜索String中的第一个SubString实例
String.lastIndexOf(Substring,Start) 是搜索String中的最后一个SubString实例
如果没有在字符串中找到到字符,就返回-1
知识点四:
要在字符串中查找某个子字符串的所有实例,需要循环整个字符串。
知识点五:
charAt()方法用于返回位于字符串中指定位置的单个字符。
例子:
<html>
<head>
<title>Listing 15.5. The charAt() Method</title>
</head>
<body>
<script language="JavaScript" type="text/javascript">
<!--
// Set up an array of test strings
var string_array = new Array(4)
string_array[0] = "Not this one."
string_array[1] = "Not this one, either."
string_array[2] = "1. Step one."
string_array[3] = "Shouldn't get this far."
// Loop through the array
for (var Counter = 0; Counter < 4; Counter++) {
// Get the first character of the string
first_char = string_array[Counter].charAt(0)
// If it's a number, break because that's the one we want
if (!isNaN(first_char)) { break }
}
alert("Here\'s the one: \"" + string_array[Counter] + "\"")
//-->
</script>
</body>
</html>
知识点六:
slice()方法是从字符串中“切”出一部分
String.slice(Start,End)
注意处:通过该方法提取出的子字符串,从Start开始,到End结束,但一定要注意不包括End
注意处:该方法允许End使用负数,方便从后面开始取值。
例子:
<html>
<head>
<title>Listing 15.6. The slice() Method</title>
</head>
<body>
<pre>
Using JavaScript
0123456789012345
</pre>
<script language="JavaScript" type="text/javascript">
<!--
var book_name = "Using JavaScript"
document.write("slice(0, 5) returns " + book_name.slice(0, 5))
document.write("<br>")
document.write("slice(6, 10) returns " + book_name.slice(6, 10))
document.write("<br>")
document.write("slice(10) returns " + book_name.slice(10))
document.write("<br>")
document.write("slice(0, -6) returns " + book_name.slice(0, -6))
//-->
</script>
</body>
</html>
知识点七:
split()方法用于分开一个字符串,并将各部分存入一个数组中。
String.split(Separator,Limit) Limit(可选)表示存入数组的项的最大数目。
如果想将字符串中每一个字符都作为一个单独的数组项,使用空字符串("")作为Separator的值。
例子:
<html>
<head>
<title>Listing 15.7. The split() Method</title>
</head>
<body>
<script language="JavaScript" type="text/javascript">
<!--
var string1 = "Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday"
var string2 = "ABCDEF"
document.write("string1 = " + string1 + "<p>")
var string1_array = string1.split(",")
for (var Counter = 0; Counter < string1_array.length; Counter++) {
document.write("string1_array[" + Counter + "] = " +
string1_array[Counter])
document.write("<br>")
}
document.write("<p>")
document.write("string2 = " + string2 + "<p>")
var string2_array = string2.split("", 4)
for (Counter = 0; Counter < string2_array.length; Counter++) {
document.write("string2_array[" + Counter + "] = " +
string2_array[Counter])
document.write("<br>")
}
//-->
</script>
</body>
</html>
知识点八:
substr()方法是如果想提取一个子字符串并且知道子字符串的长度,使用substr()是最好的选择。
String.substr(Index,Length)
例子:
<html>
<head>
<title>Listing 15.8. The substr() Method</title>
</head>
<body>
<pre>
Using JavaScript
0123456789012345
</pre>
<script language="JavaScript" type="text/javascript">
<!--
var book_name = "Using JavaScript"
document.write("substr(0, 5) returns " + book_name.substr(0, 5))
document.write("<br>")
document.write("substr(6, 4) returns " + book_name.substr(6, 4))
document.write("<br>")
document.write("substr(10) returns " + book_name.substr(10))
//-->
</script>
</body>
</html>
知识点九:
substring()方法用于从字符串中提取子字符串。
String.substring(Start,End)
例子:
html>
<head>
<title>Listing 15.9. The substring() Method</title>
</head>
<body>
<pre>
Using JavaScript
0123456789012345
</pre>
<script language="JavaScript" type="text/javascript">
<!--
var book_name = "Using JavaScript"
document.write("substring(0, 5) returns " + book_name.substring(0, 5))
document.write("<br>")
document.write("substring(6, 10) returns " + book_name.substring(6, 10))
document.write("<br>")
document.write("substring(10) returns " + book_name.substring(10))
//-->
</script>
</body>
</html>
知识点十:
其他String对象的方法:
String.concat(String2) 将String2连接到String尾部。
String.replace(rgExp,replaceText) 返回根据正则表达式进行文字替换后的字符串的复制。后替前。
String.search(rgExp) 返回与正则表达式查找内容匹配的第一个子字符串的位置。
String.toLowerCase() 转换成小写
String.toUpperCase() 转换成大写
String对象和其他类型的字符串没有实际的不同。
var book_name = new String("My Name is Tony");
var book_name = "My Name is Tony";
知识点二:
String对象提供的唯一独特的属性是prototype,它能用于增加自定义方法到String对象。
知识点三:
String.indexOf(Substring,Start) 是搜索String中的第一个SubString实例
String.lastIndexOf(Substring,Start) 是搜索String中的最后一个SubString实例
如果没有在字符串中找到到字符,就返回-1
知识点四:
要在字符串中查找某个子字符串的所有实例,需要循环整个字符串。
知识点五:
charAt()方法用于返回位于字符串中指定位置的单个字符。
例子:
<html>
<head>
<title>Listing 15.5. The charAt() Method</title>
</head>
<body>
<script language="JavaScript" type="text/javascript">
<!--
// Set up an array of test strings
var string_array = new Array(4)
string_array[0] = "Not this one."
string_array[1] = "Not this one, either."
string_array[2] = "1. Step one."
string_array[3] = "Shouldn't get this far."
// Loop through the array
for (var Counter = 0; Counter < 4; Counter++) {
// Get the first character of the string
first_char = string_array[Counter].charAt(0)
// If it's a number, break because that's the one we want
if (!isNaN(first_char)) { break }
}
alert("Here\'s the one: \"" + string_array[Counter] + "\"")
//-->
</script>
</body>
</html>
知识点六:
slice()方法是从字符串中“切”出一部分
String.slice(Start,End)
注意处:通过该方法提取出的子字符串,从Start开始,到End结束,但一定要注意不包括End
注意处:该方法允许End使用负数,方便从后面开始取值。
例子:
<html>
<head>
<title>Listing 15.6. The slice() Method</title>
</head>
<body>
<pre>
Using JavaScript
0123456789012345
</pre>
<script language="JavaScript" type="text/javascript">
<!--
var book_name = "Using JavaScript"
document.write("slice(0, 5) returns " + book_name.slice(0, 5))
document.write("<br>")
document.write("slice(6, 10) returns " + book_name.slice(6, 10))
document.write("<br>")
document.write("slice(10) returns " + book_name.slice(10))
document.write("<br>")
document.write("slice(0, -6) returns " + book_name.slice(0, -6))
//-->
</script>
</body>
</html>
知识点七:
split()方法用于分开一个字符串,并将各部分存入一个数组中。
String.split(Separator,Limit) Limit(可选)表示存入数组的项的最大数目。
如果想将字符串中每一个字符都作为一个单独的数组项,使用空字符串("")作为Separator的值。
例子:
<html>
<head>
<title>Listing 15.7. The split() Method</title>
</head>
<body>
<script language="JavaScript" type="text/javascript">
<!--
var string1 = "Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday"
var string2 = "ABCDEF"
document.write("string1 = " + string1 + "<p>")
var string1_array = string1.split(",")
for (var Counter = 0; Counter < string1_array.length; Counter++) {
document.write("string1_array[" + Counter + "] = " +
string1_array[Counter])
document.write("<br>")
}
document.write("<p>")
document.write("string2 = " + string2 + "<p>")
var string2_array = string2.split("", 4)
for (Counter = 0; Counter < string2_array.length; Counter++) {
document.write("string2_array[" + Counter + "] = " +
string2_array[Counter])
document.write("<br>")
}
//-->
</script>
</body>
</html>
知识点八:
substr()方法是如果想提取一个子字符串并且知道子字符串的长度,使用substr()是最好的选择。
String.substr(Index,Length)
例子:
<html>
<head>
<title>Listing 15.8. The substr() Method</title>
</head>
<body>
<pre>
Using JavaScript
0123456789012345
</pre>
<script language="JavaScript" type="text/javascript">
<!--
var book_name = "Using JavaScript"
document.write("substr(0, 5) returns " + book_name.substr(0, 5))
document.write("<br>")
document.write("substr(6, 4) returns " + book_name.substr(6, 4))
document.write("<br>")
document.write("substr(10) returns " + book_name.substr(10))
//-->
</script>
</body>
</html>
知识点九:
substring()方法用于从字符串中提取子字符串。
String.substring(Start,End)
例子:
html>
<head>
<title>Listing 15.9. The substring() Method</title>
</head>
<body>
<pre>
Using JavaScript
0123456789012345
</pre>
<script language="JavaScript" type="text/javascript">
<!--
var book_name = "Using JavaScript"
document.write("substring(0, 5) returns " + book_name.substring(0, 5))
document.write("<br>")
document.write("substring(6, 10) returns " + book_name.substring(6, 10))
document.write("<br>")
document.write("substring(10) returns " + book_name.substring(10))
//-->
</script>
</body>
</html>
知识点十:
其他String对象的方法:
String.concat(String2) 将String2连接到String尾部。
String.replace(rgExp,replaceText) 返回根据正则表达式进行文字替换后的字符串的复制。后替前。
String.search(rgExp) 返回与正则表达式查找内容匹配的第一个子字符串的位置。
String.toLowerCase() 转换成小写
String.toUpperCase() 转换成大写