"JavaScript learning"-js built-in Object 3-string object

Source: Internet
Author: User

Defined:

The way to define a string is to assign a value directly, for example: Var mystr= "Javascript is good!";

To access the properties of a string:

    1. Length Property

Eg:var myl=mystr.length;//The myl length is 19 (spaces, symbols to be counted in, the end of the same as the C language and there is a null for a bit)

Ways to access strings:

    • toUpperCase () Method: Convert characters to uppercase

eg:var mystr= "Javascript"; var upstr=mystr.touppercase ();//The value of UPSTR is: javascript;2

    • toLowerCase () Method: Convert a string to a lowercase letter

Eg:var mystr= "Javascript";

var lowstr=mystr.tolowercase (); The value of the LOWSTR is: JavaScript;

    • CharAt () Method: Returns the character at the specified position

var mystr= "Javascript";

var Thechar=mystr.charat (parameter)

Parameter: The subscript of the character in the string, the range is 0~ string length-1, out of range returns null

    • IndexOf () Method: Returns the position of the first occurrence of a specified string value in a string (returns the subscript for the first position, not including the return-1)

Syntax: var mystr= "Javascript";

var thechar=mystr.indexof (parameter substring, parameter startpos)

Parameter substring: substring to retrieve

Parameter startpos: (Optional integer argument) the position at which to start the retrieval, is the subscript of the string, and the argument is omitted starting from the first character of the string

Eg: Use the indexof () method to retrieve the position where the second character o appears:

1 <Scripttype= "Text/javascript">2   varmystr="Hello world!"3   varFirstpos=Mystr.indexof ('o');4 document.write (Mystr.indexof ('o', Firstpos+1));5 </Script>

The result is: 7; If the parameter startpos is Firstpos, then 4 will be returned, since the fourth bit is the position of the first O, including the start position itself

    • Split () Method: Splits a string into an array of strings and returns this array

Syntax: mystr.split (parameter separator, parameter limit);

Parameter separator: Split from the location specified by this parameter, and if an empty string ("") is used as separator, then each character in the string is split.

Parameter limit: The number of substrings returned is not more than this parameter

eg

1 <script type= "Text/javascript" >2     var mystr= "86-010-8546"; 3     document.write (Mystr.split ('-') + "<br/>"); 4     document.write (Mystr.split (") +" <br/> "); 5     document.write (Mystr.split ("', 3)"          ); 6 </script>

Results: 86,010,8546

8,6,-, 0,1,0,-, 8,5,4,6

8,6,-

substring () method: Extracts the character of a string intermediary between two specified subscripts, all characters from start (containing the start position character) to stop-1, with a length of stop minus start.

Syntax: stringobject.substring (Starpos,stoppos)

Starpos parameters: Required. Start position

Stoppos parameter: Optional. End position, do not write this argument returns the substring until the end of the string

If start is larger than stop, the method swaps both parameters before extracting the substring.

Eg: extracts world! substring, hello substring from Hello world!.

1 <script type= "Text/javascript" >2     var mystr= "Hello world!" 3     document.write (mystr.substring (6) + "<br/>"); 4     document.write (mystr.substring (0,5)); 5 </script>
    • The substr () method extracts a specified number of strings from the string that start at the startpos position.

Syntax: Stringobject.substr (startpos,length)
Starpos parameters: Required. Start position. If a negative number, such as-1, is calculated from the first countdown. If negative and the absolute value is greater than the string length, then Starpos is 0.

Length parameter: Extract the lengths of the strings,

Eg: extracts world! substring, hello substring from Hello world!.

1 <script type= "Text/javascript" >2var mystr= "Hello world!" ; 3 document.write (MYSTR.SUBSTR (6) + "<br/>");//or ( -6,6)4 document.write (   Mystr.substr ( -6,6)  5 </script>

Code Volume: 29 Lines
Date: 2016.6.3
Zeng Su
For reprint or use please contact email ([email protected])

Stringobject.indexof (substring, startpos)

"JavaScript learning"-js built-in Object 3-string object

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.