Javascript--string String Object Properties

Source: Internet
Author: User

The properties of the Access string object length:

Stringobject.length; Returns the length of the string.

var mystr= "Hello world!"; var myl=mystr. length ;

When the above code executes, the value of MYL will be: 12

Methods for accessing String objects:

Use the toUpperCase () method of the string object to convert the string lowercase letters to uppercase:

var mystr= "Hello world!"; var mynum=mystr.touppercase ();
After executing the above code, the value of Mynum is: HELLO world!

Returns the character at the specified position

The CharAt () method returns the character at the specified position. The returned character is a string of length 1.

Grammar:

Stringobject.charat (Index)

Parameter description:


Note : 1. The subscript for the first character in a string is 0. The subscript for the last character is the string length minus one (string.length-1).

2. If the parameter index is not between 0 and String.length-1, the method returns an empty string.

For Example: in the string "I love javascript!", return the character of position 2:

<script type= "Text/javascript" >  var mystr= "I Love javascript!"  document.write (Mystr.charat (2));</script>

Note: a space is also counted as a character.

Running result of the above code:

L

Returns the location of the first occurrence of the specified string

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

Grammar

Stringobject.indexof (substring, startpos)

Parameter description:


Description

1. The method will retrieve the string stringobject from beginning to end to see if it contains substring substring.

2. Optional parameters, starting from the startpos position of the stringobject to find substring, if no this parameter will be found from the beginning of the Stringobject.

3. If a substring is found, the position of the first occurrence of the substring is returned. The character position in Stringobject is starting at 0.

Note: The 1.indexOf () method is case-sensitive.

2. If the string value that you want to retrieve does not appear, the method returns-1.

For example: different searches within the "I Love javascript!" string:

<script type= "Text/javascript" >  var str= "I Love javascript!"  document.write (Str.indexof ("I") + "<br/>");  document.write (Str.indexof ("V") + "<br/>");  document.write (Str.indexof ("V", 8));</script>

The output of the above code:

049

String Split Split ()

Knowledge Explanation:

The split () method splits a string into an array of strings and returns this array.

Syntax:
Stringobject.split (Separator,limit)
parameter Description:

Note: If you use an empty string ("") as a separator, then each character in the Stringobject will be split.

We'll split the string in different ways:

Use the specified symbol to split the string with the following code:

var mystr = "www.imooc.com";d ocument.write (Mystr.split (".") + "<br>");d Ocument.write (Mystr.split (".", 2) + "<br>");

Operation Result:

Www,imooc,comwww,imooc

To divide a string into characters, the code is as follows:

document.write (Mystr.split ("") + "<br>");d Ocument.write (Mystr.split ("", 5));

Operation Result:

W,w,w,., i,m,o,o,c,., c,o,mw,w,w,., I

Extract string substring ()

The substring () method is used to extract the character of a string intermediary between two specified subscripts.

Grammar:

Stringobject.substring (Starpos,stoppos)

Parameter description:

Attention:

1. The returned content is all characters from start (the character containing the start position) to Stop-1, whose length is stop minus start.

2. If the parameter start is equal to stop, then the method returns an empty string (that is, a string of length 0).

3. If start is larger than stop, the method will exchange the two parameters before extracting the substring.

Use SUBSTRING () to extract the string from the string, with the following code:

<script type= "Text/javascript" > var mystr= "I love JavaScript";  document.write (mystr.substring (7)); document.write (mystr.substring (2,6));</script>

Run Results :

Javascriptlove

Extracts the specified number of characters substr ()

The substr () method extracts a specified number of strings from the string that start at the startpos position.

Grammar:

Stringobject.substr (Startpos,length)

Parameter description:

Note: If the parameter startpos is a negative number, the position is calculated from the end of the string. That is,-1 refers to the last character in the string, 2 refers to the second-lowest character, and so on.

If Startpos is negative and the absolute value is greater than the string length, Startpos is 0.

Use SUBSTR () to extract some characters from the string, with the following code:

<script type= "Text/javascript" >  var mystr= "I love javascript!";  document.write (MYSTR.SUBSTR (7));  document.write (Mystr.substr (2,4));</script>

Operation Result:

Javascript!love


Javascript--string String Object Properties

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.