String object knowledge point summary

Source: Internet
Author: User
Knowledge Point 1:
The string object is not actually different from other types of strings.
VaR book_name = new string ("My name is Tony ");
VaR book_name = "My name is Tony ";

Knowledge Point 2:
The unique attribute provided by the string object is prototype, which can be used to add custom methods to the string object.

Knowledge Point 3:
String. indexof (substring, start) is the first substring instance in string search.
String. lastindexof (substring, start) is the last substring instance in the search string.
If no character is found in the string,-1 is returned.

Knowledge Point 4:
To search for all instances of a substring in a string, you need to loop the entire string.

Knowledge Point 5:
The charat () method is used to return a single character at the specified position in the string.
Example:
<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>

Knowledge Point 6:
The slice () method is to "cut" a part from the string
String. Slice (START, end)
Note: The substring extracted by this method starts from start and ends with end, but it must not include end.
Note: This method allows the end to use a negative number, which is convenient to start from the end.
Example:
<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>

Knowledge Point 7:
The split () method is used to separate a string and store each part in an array.
String. Split (separator, limit) limit (optional) indicates the maximum number of items stored in the array.
To use each character in a string as a separate array, use an empty string ("") as the value of the separator.
Example:
<HTML>
<Head>
<Title> listing15.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 stringpattern array = string1.split (",")
For (VAR counter = 0; counter <stringincluarray.length; counter ++ ){
Document. Write ("stringpolicarray [" + counter + "] =" +
Stringcounter 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>

Knowledge point 8:
The substr () method is the best choice if you want to extract a substring and know its length.
String. substr (index, length)
Example:
<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>

Knowledge Point 9:
The substring () method is used to extract a substring from a string.
String. substring (START, end)
Example:
HTML>
<Head>
<Title> listing15.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>

Knowledge Point 10:
Methods for other string objects:
String. Concat (string2) connects string2 to the end of string.
String. Replace (rgexp, replacetext) returns a copy of the string after text replacement based on the regular expression. Before replacement.
String. Search (rgexp) returns the position of the first substring that matches the content found in the regular expression.
String. tolowercase () to lowercase
String. touppercase () to uppercase

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.