HTML Dom tutorial 50-javascript String object

Source: Internet
Author: User
Tags split words

HTML Dom tutorial 50-javascript String object

 

A string is a basic data type of JavaScript.

The Length attribute of the string object declares the number of characters in the string. The string class defines a large number of string operations.

It should be noted that the javascript string is immutable, and the methods defined by the string class cannot change the content of the string.

1: String object Method
Method Description FF N IE
Anchor () Create an HTML anchor. 1 2 3
Big () Use a large font to display strings. 1 2 3
Blink () Displays a flashing string. 1 2  
Bold () Use bold to display strings. 1 2 3
Charat () Returns the characters at the specified position. 1 2 3
Charcodeat () Returns the Unicode encoding of characters at the specified position. 1 4 4
Concat () The connection string. 1 4 4
Fixed () Displays strings in typewriter text. 1 2 3
Fontcolor () Use the specified color to display the string. 1 2 3
Fontsize () Use the specified size to display the string. 1 2 3
Fromcharcode () Creates a string from the character encoding. 1 4 4
Indexof () Returns a string. 1 2 3
Italics () Use italics to display strings. 1 2 3
Lastindexof () Search for strings from the back and forward. 1 2 3
Link () Display the string as a link. 1 2 3
Localecompare () Compares two strings in a specific local order. 1 4 4
Match () Find the matching of one or more expressions. 1 4 4
Replace () Replace the substring that matches the regular expression. 1 4 4
Search () Returns the value that matches the regular expression. 1 4 4
Slice () Extract the string segment and return the extracted part in the new string. 1 4 4
Small () Use a small font size to display strings. 1 2 3
Split () Splits a string into a string array. 1 4 4
Strike () Use strikethrough to display strings. 1 2 3
Sub () Returns the string as a subscript. 1 2 3
Substr () Extracts a specified number of characters from the start index number. 1 4 4
Substring () Extract the characters between two specified index numbers in the string. 1 2 3
Sup () Displays the string as a superscript. 1 2 3
Tolocalelowercase () Converts a string to lowercase. - - -
Tolocaleuppercase () Converts a string to uppercase. - - -
Tolowercase () Converts a string to lowercase. 1 2 3
Touppercase () Converts a string to uppercase. 1 2 3
Tosource () Representing the objectSource code. 1 4 -
Tostring () Returns a string. - - -
Valueof () Returns the original value of a string object. 1 2 4
2: attributes of the string object
Attribute Description FF N IE
Constructor Reference to the function that creates the object 1 4 4
Length String Length 1 2 3
Prototype Allows you to add attributes and methods to an object 1 2 4

 

3: Anchor () method

In this example, we will add an anchor for the text:

<SCRIPT type = "text/JavaScript">

VaR TXT = "Hello world! "
Document. Write (txt. Anchor ("myanchor "))

</SCRIPT>

The aboveCodeThe output is pure HTML:

<A name = "myanchor"> Hello world! </A>

4: Blink (), big (), sub (), fixed () and other methods

<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.w3school.com.cn") + "</P> ")
</SCRIPT>

</Body>
</Html>

5: Concat () method

In this example, we will create two strings and then use Concat () to display them as a string:

 
<SCRIPT type = "text/JavaScript">

VaR str1 = "hello"
VaR str2 = "world! "
Document. Write (str1.concat (str2 ))

</SCRIPT>

The output of the above Code is:

 
Hello world!

6: indexof () method

NOTE 1: The indexof () method is case sensitive!

NOTE 2: If the string value to be retrieved does not appear, this method returns-1.

In this example, we will! "Different searches within the string:

 
<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>

Output of the above Code:

 
0
-1
6

 7: substring () method

Syntax: stringobject. substring (start, stop): If the stop parameter is omitted, the returned substring ends to the end of the string. If the start parameter is equal to the end parameter, this method returns an empty string (a string with a length of 0 ). If start is greater than end, this method swaps the two parameters before extracting the substring.

Important: Unlike slice () and substr (), substring () does not accept negative parameters.

In this example, we use substring () to extract some characters from the string:

 
<SCRIPT type = "text/JavaScript">

VaR STR = "Hello world! "
Document. Write (Str. substring (3 ))

</SCRIPT>

Output:

 
Lo world!

8: Split () method

Syntax: stringobject. Split (Separator,Howmany): ParameterHowmanyYou can specify the maximum length of the returned array. If this parameter is set, no more substrings are returned than the array specified by this parameter. If this parameter is not set, the entire string is split, regardless of its length.

Note: If the empty string ("") is usedSeparator, Each character in the stringobject will be split.

Example 1

In this example, we will split the string in different ways:

<SCRIPT type = "text/JavaScript">

VaR STR = "How are you doing today? "

Document. Write (Str. Split ("") + "<br/> ")
Document. Write (Str. Split ("") + "<br/> ")
Document. Write (Str. Split ("", 3 ))

</SCRIPT>

Output:

 
How, are, you, doing, today?
H, O, W, A, R, E, Y, O, U, D, o, I, n, G, T, O, D,, Y ,?
How, are, you
Example 2

In this example, we split strings with more complex structures:

 
"2: 3: 4: 5". Split (":") // Returns ["2", "3", "4", "5"]
"| A | B | C". Split ("|") // ["", "A", "B", "C"]
Example 3

Use the following code to split sentences into words:

 
VaR words = sentence. Split ('')

Or use the regular expression as the separator:

 
VaR words = sentence. Split (/"s + /)
Example 4

If you want to split words into letters or strings into characters, use the following code:

 
"Hello". Split ("") // you can return ["H", "E", "L", "L", "O"]

If you only need to return a part of the characters, use the howmany parameter:

"Hello". Split ("", 3) // you can return ["H", "E", "L"]

 9: Match () method

The match () method can be used to search for a specified value in a string or to find a match between one or more regular expressions. This method is similar to indexof () and lastindexof (), but it returns the specified value rather than the position of the string.

Syntax: stringobject. Match (searchvalue), where searchvalue is required. Specifies the string value to be retrieved.

Or stringobject. Match (Regexp), where Regexp is required. Specifies the Regexp object of the pattern to be matched. If this parameter is not a Regexp object, you must first pass it to the Regexp constructor and convert it to a Regexp object.

The match () method retrieves the stringobject string to find one or more texts that match Regexp. The behavior of this method depends largely on whether Regexp has a flag.

If Regexp does not mark g, the match () method can only perform one match in stringobject. If no matching text is found, match () returns NULL. Otherwise, it returns an array containing information related to the matched text it finds. The 0th elements in the array store the matching text, while the remaining elements store the text that matches the regular expression's subexpression. In addition to these regular array elements, the returned array also contains two object attributes. The index attribute declares the position of the starting character of the matching text in the stringobject, And the INPUT attribute declares the reference to the stringobject.

If Regexp has a flag, the match () method performs a global search and finds all matching substrings in stringobject. If no matched substring is found, null is returned. If one or more matched substrings are found, an array is returned. However, the content of the array returned by global match is very different from that returned by the former. Its array elements store all matched substrings in stringobject, and there is no index or INPUT attribute.

Note: In global search mode, match () does not provide text information that matches the subexpression, nor declare the position of each matched substring. You can use regexp.exe C () to retrieve global information ().

Example 1

In this example, we will! "For different searches:

 
<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>

Output:

 
World
Null
Null
World!
Example 2

In this example, we use a globally matched regular expression to retrieve all numbers in the string:

<SCRIPT type = "text/JavaScript">

VaR STR = "1 plus 2 equal 3"
Document. Write (Str. Match (/"d +/g))

</SCRIPT>

Output:

 
1, 2, 3

 

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.