Object-oriented knowledge js-built-in objects

Source: Internet
Author: User

String

String object constructor--strings as objects

Traditional way: var Zhangsan = ' Zhang San ';

By object form: var lisi = new String (' John Doe ');

Overview of String Object properties and methods

Gets the length of string lengths property
Connection string: concat
Get index value: IndexOf ()
Get a single character based on the index value: charAt ()

Concat method

    var s1= "a";    var s2= "B";    var s3= "C";    Console.log (S1.concat (S2,S3));//abc    var arr = [1, 2, 3];    Console.log (Arr.concat (4, 5));//[1,2,3,4,5]

indexOf--- find a match returns the index value if not found return-1

    var mystring= "JavaScript";    var a1=mystring.indexof ("V"),//2    var a2=mystring.indexof ("S"),//4    var a3=mystring.indexof ("Script");//4    var a4=mystring.indexof ("key");//If no match is returned-1

Complete indexof Usage

You can pass in the second parameter: indexOf (Str,fromindex)
Indicates that the lookup starts at index position fromindex, and if Fromindex is omitted, the default is to start from index 0.
If Fromindex is negative, the lookup starts at index 0.

    var B1 = Mystring.indexof ("V", 5);//starting from Subscript 5, and V in index 2, so not found, return-1    var b2 = mystring.indexof ("V", 1);//-2    Console.log (B1)//-1    console.log (B2)//-2

charAt---- returns the character at the specified index and returns an empty string if the index is out of bounds.

Returns the character of the specified index position
(because there is no character type in JavaScript, a string of length 1 is returned).

    Mystring= "JavaScript";    Console.log (Mystring.charat (1));//a    Console.log (Mystring.charat (10000000000000000));//If the index is out of bounds, return an empty string    Console.log (Mystring.charat (-1));//If the index is out of bounds, return an empty string  --""    Console.log (Mystring.charat (- 10000000000000000000000));//If the index is out of bounds, return an empty string

substr (fromindex,length) gets a partial string, starting from the starting index fromIndex the length of the string

Mystring= "JavaScript";

Intercept length from start index Fromindex string-forward intercept

    Console.log (MYSTRING.SUBSTR);//a          means intercept from the 1th index value, capturing 1 characters    Console.log (MYSTRING.SUBSTR);//av Women's worries     represents a 2-character intercept starting from the 1th index value

If length is not specified or length exceeds the maximum length that can be intercepted, the end is truncated.

    Console.log (MYSTRING.SUBSTR (1));//avascript    Console.log (Mystring.substr (1,4000000));//avascript

Reverse Intercept
If the starting index is negative, the intercept starts from right to left -1 means the penultimate, 2 is the second-to-last

Mystring= "JavaScript";

    Console.log (Mystring.substr ( -1,1)),//t  means intercepting 1 characters from the countdown to the first    Console.log (Mystring.substr ( -2,1));//p  The Console.log is intercepted from the second-to-last intercept, intercepting 1 characters    (MYSTRING.SUBSTR ( -6,1)),//s  means intercepting from the bottom 6th, intercepting 1 characters    Console.log ( Mystring.substr ( -6,2));//sc  says intercept from the bottom 6th, intercept 2 characters

substring (startindex,endindex) gets a partial string

Intercepts the substring from the starting index startindex to the end index Endindex,
The result contains the character at startindex and does not contain the character at Endindex.

    If the number is omitted, all subsequent     console.log (Mystring.substring (4)) are obtained automatically,    and//script//if startindex or endindex is negative, it is replaced with 0.    Console.log (mystring.substring ( -1,1));//j    //If startindex = EndIndex, an empty string is returned.    Console.log (mystring.substring (3,3));//returns NULL    //If startindex > EndIndex, two values are exchanged when the method is executed.    Console.log (mystring.substring (3,1));//equivalent to mystring.substring (1,3)

Slice (startindex,endindex) gets a partial string

Intercepts the substring from the starting index startindex to the end index Endindex,
The result contains the character at startindex and does not contain the character at Endindex.

    Mystring= "JavaScript";    Console.log (Mystring.slice (1,3))//av    Console.log (Mystring.slice (4,5))//s    Console.log (Mystring.slice ( 4))//script  //If the number is omitted, automatically gets all subsequent

The basic usage is the same as the substring usage, and the differences are as follows:

Stringobj.slice (start, [end])
If start is negative, it is processed as length + start, where length is the number of the array.
Console.log (Mystring.slice ( -1,3))
If end is negative, it is treated as a length + end, where long is the number of the array.
Console.log (Mystring.slice (2,-3))
If End is omitted, then the slice method is copied to the end of arrayobj.
If end is greater than start, no elements are copied into the new array.
//
Strvariable.substring (start, end)
If start or end is NaN or negative, replace it with 0.
The length of the substring equals the absolute value of the difference between start and end. For example, the length of the substring returned in strvar.substring (0, 3) and strvar.substring (3, 0) is 3.
Slice can operate on the array, substring not ...

Split () split 

Splits a given string, returning a string array of multiple strings that have been split.

    Console.log (' Split () usage ')    var s= "a,bc,d";    Console.log (S.split (","));//["A", "BC", "D"]    s= "A1B1C1D1";    Console.log (S.split ("1"));//["A", "B", "C", "D", "" "

John () Merge 

Merges an array into a string using the delimiter of your choice

    var mylist=new Array ("JPG", "BMP", "GIF", "ico", "PNG");    var imgstring=mylist.join ("|"); /result is jpg|bmp|gif|ico|png

Split () can also be combined with regular expressions

    MyString = ' JavaScript is a good script language ';    Console.log (Mystring.split (/\s/)); Javascript,is,a,good,script,language    //incoming \s means to match a space, we divide the string into an array, if you want to access a certain, it can be clearly pointed out:    Console.log ( Mystring.split (/\s/) [3]); Good

String Case Conversion
Merges an array into a string using the delimiter of your choice

    Mystring= "JavaScript";    myString = Mystring.tolowercase ();//javascript     myString = Mystring.touppercase ();//javascript

  

 

  

Object-oriented knowledge js-built-in objects

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.