JavaScript String Object Detailed _ Basics

Source: Internet
Author: User
Tags lowercase numeric value

A string object is used to process text (strings).

1. Create string

Copy Code code as follows:
var str = "Hello world";
var str1 = new String (str);
var str = String ("Hello World");

All three methods are OK, but slightly different

2, Length Property---> Return string lengths

3. The anchor () method is used to create an HTML anchor.

What is an HTML anchor?
It's <a ...>...</a>.
Stringobject.anchor (Anchorname)
Anchorname required. Define the name for the anchor.

Copy Code code as follows:
var txt= "Hello world!"
document.write (Txt.anchor ("Myanchor"))

The output is:

<a name= "Myanchor" >hello world!</a>

Of course, what you see in the browser is Hello world!.

4. The big () method is used to display the string as a large font.

How to use:

Copy Code code as follows:
var str= "Hello world!"
document.write (Str.big ())

5. The blink () method is used to display a flashing string.

How to use:

Copy Code code as follows:
var str= "Hello world!"
document.write (Str.blink ())

6. The bold () method is used to display the string as bold.

Copy Code code as follows:
var str= "Hello world!"
document.write (Str.bold ())

7. The CharAt () method returns the character at the specified position.

Stringobject.charat (Index)
Index required. A number that represents a position in a string, that is, the subscript of a character in a string.
Note: The subscript for the first character in the string is 0. If the argument index is not between 0 and String.Length, the method returns an empty string.

8. The charCodeAt () method returns the Unicode encoding of the character at the specified position. This return value is an integer between 0-65535.

The method charCodeAt () is similar to the operation performed by the CharAt () method, except that the former returns the encoding of the character at the specified position, and the latter returns a string of characters subcode.

Copy Code code as follows:
var str= "Hello world!"
document.write (Str.charcodeat (1))

Output as: 101

9. The concat () method is used to connect two or more strings.

Stringobject.concat (Stringx,stringx,..., stringx)
Stringx required. One or more string objects that will be concatenated to a string.
Tip: Note that it is usually easier to use the "+" operator for concatenation of strings.

Copy Code code as follows:
var str1= "Hello"
var str2= "world!"
document.write (Str1.concat (STR2))

The output is:
Hello world!

10. The FontColor () method is used to display the string according to the specified color.

Stringobject.fontcolor (color)
Color required. Font-color for string rules. The value must be a color name (red), an RGB value (RGB (255,0,0)), or a hexadecimal number (#FF0000).

Cases:

Copy Code code as follows:
var str= "Hello world!"
document.write (Str.fontcolor ("Red"))

11. The LastIndexOf () method returns the last occurrence of a specified string value, which is searched forward at the specified position in a string.

Stringobject.lastindexof (Searchvalue,fromindex)
Searchvalue required. A string value that requires retrieval.
Fromindex an optional integer parameter. Specify where to start retrieving in the string. Its legal value is 0 to stringobject.length-1. If this argument is omitted, the retrieval begins at the last character of the string.
Note: the LastIndexOf () method is case sensitive!
If the string value to retrieve does not appear, the method returns-1.

Cases:

Copy Code code as follows:
var str= "Hello world!"
document.write (Str.lastindexof ("Hello") + "<br/>")
document.write (Str.lastindexof ("World") + "<br/>")
document.write (Str.lastindexof ("World"))

Output:

0
-1
6

12. The link () method is used to display the string as a hyperlink.

Stringobject.link (URL)//url required. Specify the URL to link to.
var str= "cloud-dwelling community"
document.write (Str.link ("Http://www.jb51.net"))

13. The match () method retrieves the specified value within the string, or finds a match for one or more regular expressions.

The method is similar to IndexOf () and LastIndexOf (), but it returns the specified value, not the position of the string.
Stringobject.match (Searchvalue)
Searchvalue required. Specify the string value to retrieve.
Or:
Stringobject.match (RegExp)
RegExp required. The RegExp object that prescribes the pattern to match. If the parameter is not a RegExp object, you need to pass it to the RegExp constructor first and convert it to a RegExp object.

Copy Code code as follows:
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!"))

The output is:

World
Null
Null
world!

14. The replace () method is used to replace some characters with some character in a string, or to replace a substring that matches a regular expression.

Stringobject.replace (regexp/substr,replacement)
Regexp/substr required. The RegExp object that prescribes the substring or pattern to be replaced.
Note that if the value is a string, it is used as the direct text pattern to retrieve, not first converted to the RegExp object.
Replacement required. A string value. Provides a function that replaces text or generates alternate text.
A new string that is replaced with replacement for the first match of RegExp or after all matches have been made.

Cases:

Copy Code code as follows:
var str= "Visit microsoft!"
document.write (Str.replace (/microsoft/, "JB51"))

The output results are:

Visit jb51!

15. The search () method retrieves the substring specified in the string, or retrieves a substring that matches the regular expression.

Stringobject.search (RegExp)
RegExp This parameter can be a substring that needs to be retrieved in Stringobject, or it can be a RegExp object that needs to be retrieved.
Note: To perform a retrieval that ignores case, append flag I.
return value:
The starting position of the first substring in the stringobject that matches the regexp.
Note: If no matching substring is found, return-1.
Note: Search () is case sensitive

Cases:
[Code]var str= "Visit jb51!"
document.write (Str.search (/jb51/))

Output:

6

16. The slice () method extracts a part of the string and returns the extracted part with a new string.

Stringobject.slice (Start,end)
The starting subscript of the fragment to be extracted by start. If it is a negative number, the parameter sets the position from the tail of the string. In other words,-1 refers to the last character of the string,-2 to the penultimate character, and so on.
End is immediately followed by the bottom of the fragment to be extracted. If this parameter is not specified, the substring to be fetched includes the string at the end of the original string. If the argument is a negative number, it sets the position from the tail of the string.
return value:
A new string. Includes all characters for the string stringobject from start (including start) to end ending (excluding ends).
String.slice () is similar to Array.slice ()

Copy Code code as follows:
var str= "Hello Happy world!"
document.write (Str.slice (6))

Output: Happy world!

Copy Code code as follows:
var str= "Hello Happy world!"
document.write (Str.slice (6,11))

Output: Happy

17. The split () method is used to split a string into an array of strings.

Stringobject.split (Separator,howmany)
Separator required. A string or regular expression that splits the Stringobject from the place specified by the parameter.
Howmany Optional. This parameter specifies the maximum length of the array to return. If this argument is set, the returned substring will not be more than the array specified by this parameter. If this argument is not set, the entire string is split, regardless of its length.
Note: If an empty string (""), not a space, is used as a separator, then each character in the Stringobject will be split between

Cases:

Copy Code code as follows:
var str= "How are your doing today?"

document.write (Str.split ("") + "<br/>")
document.write (Str.split ("") + "<br/>")
document.write (Str.split ("", 3))

Output:

How,are,you,doing,today?
H,o,w, A,r,e, Y,o,u, D,o,i,n,g, T,o,d,a,y,?
How,are,you

18. The substr () method extracts a specified number of characters from the start subscript in a string.

Stringobject.substr (Start,length)
Start Required. The starting subscript of the substring to extract. Must be a numeric value. If it is a negative number, the argument declares the position from the tail of the string. In other words,-1 refers to the last character in the string, 2 to the penultimate character, and so on.
Length is optional. The number of characters in the substring. Must be a numeric value. If this argument is omitted, the string from the start position of the stringobject to the end is returned.
return value:
A new string that contains the lenght character starting at the start of the Stringobject, including the character that the start refers to. If lenght is not specified, the returned string contains characters from the end of start to Stringobject.
Note: ECMAscript does not standardize the method and is therefore opposed to using it.

Copy Code code as follows:
var str= "Hello world!"
document.write (Str.substr (3,7))

Output:

Lo Worl

19. The substring () method is used to extract characters between the string mediation and the two specified subscript.

Stringobject.substring (Start,stop)
Start Required. A non-negative integer that stipulates the position of the first character of the substring to be extracted in the stringobject.
Stop is optional. A non-negative integer that is 1 more in the Stringobject than the last character of the substring to extract, and if omitted, the returned substring continues to the end of the string.
return value
A new string that contains a substring of stringobject whose contents are all characters from the start to the stop-1, whose length is stop minus start.
Note:
The substring returned by the substring () method includes the character at start, but does not include the character at the end.
If the argument start is equal to end, then the method returns an empty string (that is, the length 0). If start is larger than end, the method swaps the two parameters before extracting the substring.

Note:
Unlike the slice () and substr () methods, substring () does not accept negative parameters.

Cases:

Copy Code code as follows:
var str= "Hello world!"
document.write (str.substring (3,7))

Output:
Lo W

20. Definition and usage

The toLowerCase () method is used to convert a string to lowercase.
Stringobject.tolowercase ()//No Ginseng
return value:
A new string in which all uppercase characters stringobject are converted to lowercase characters.

21, the toUpperCase () method is used to convert the string to uppercase.

Stringobject.touppercase ()//No Ginseng
return value:
A new string in which all lowercase characters stringobject are converted to uppercase characters.

22. The IndexOf () method returns the position of the first occurrence of a specified string value in a string.

Stringobject.indexof (Searchvalue,fromindex)
Searchvalue required. A string value that requires retrieval.
Fromindex an optional integer parameter. Specify where to start retrieving in the string. Its legal value is 0 to stringobject.length-1. If this argument is omitted, it is retrieved from the first character of the string.

Description
This method retrieves the string stringobject from beginning to end to see if it contains substring searchvalue. The location at which to start retrieving is at the fromindex of the string or at the beginning of the string (no fromindex is specified). If a searchvalue is found, it returns the position of the first occurrence of searchvalue. The character position in the Stringobject is starting at 0.

Note: the IndexOf () method is case sensitive!

Cases:

Copy Code code as follows:
var str= "Hello world!"
document.write (Str.indexof ("Hello") + "<br/>")
document.write (Str.indexof ("World") + "<br/>")
document.write (Str.indexof ("World"))

Output:
0
-1
6

See more JavaScript syntax, you can focus on: JavaScript reference tutorial, JavaScript Code style guide, and also hope that many people support the cloud-Habitat community.

Related Article

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.