Js-20170619-string Object

Source: Internet
Author: User

The properties and methods of string: 1. Length PropertyThe Length property returns the lengths of the strings. 2. CharAt ()The Charat method returns the character at the specified position, where the parameter is numbered starting at 0. If the argument is negative, or is greater than or equal to the length of the string, Charat returns an empty string. ' abc '. CHARAT ( -1)//"' abc '. CHARAT (3)//" "3. Concat ()The concat method is used to concatenate two strings, returning a new string without changing the original string. var S1 = ' abc '; var s2 = ' def '; S1.concat (S2)//"abcdef" S1//"ABC" the method can accept multiple parameters. ' A '. Concat (' B ', ' C ')//"ABC" if the argument is not a string, the Concat method converts the its first to a string and then connects. var one = 1;var-2;var three = ' 3 '; ". Concat (one, both, three)//" 123 "one + one + three//" 33 "4. Slice ()The slice method is used to remove substrings from the original string and return without altering the original string. its first argument is the starting position of the substring, and the second argument is the end of the substring (without that position). ' JavaScript '. Slice (0, 4)//"Java"If you omit the second argument, it indicates that the substring has ended until the original string. ' JavaScript '. Slice (4)//"Script"If the argument is a negative value, it represents the position at which the inverse is calculated from the end, that is, the negative value plus the string length. ' JavaScript '. Slice ( -6)//"Script" ' JavaScript '. Slice (0,-6)//"Java" ' JavaScript '. Slice ( -2,-1)//"P"If the first argument is greater than the second argument, the slice method returns an empty string. ' JavaScript '. Slice (2, 1)//"" 5. IndexOf (), LastIndexOf ()These two methods are used to determine the position of a string in another string, returning an integer that represents where the match began. If 1 is returned, the mismatch is indicated. The difference between the two is that indexof begins to match from the head of the string and LastIndexOf matches from the tail. ' Hello World '. IndexOf (' o ')//4 ' JavaScript '. IndexOf (' script ')//-1 ' Hello World '. LastIndexOf (' o ')//7 They can also accept the second parameter, For the IndexOf method, the second parameter represents a backward match from that position, and for LastIndexOf, the second parameter represents a forward match from that position. "Hello World". IndexOf (' O ', 6)//7 ' Hello World '. LastIndexOf (' O ', 6)//46. Trim ()The trim method is used to remove spaces at both ends of a string, returning a new string without altering the original string. ' Hello World '. Trim ()//"Hello World" This method removes not only spaces, but also tabs (\ t, \v), newline characters (\ n), and carriage return (\ r). ' \R\NABC \ t '. Trim ()//' ABC ' 7. toLowerCase (), toUpperCase ()The toLowerCase method is used to convert a string to lowercase, and touppercase all to uppercase. They all return a new string without changing the original string. ' Hello World '. toLowerCase ()//"Hello World" ' Hello World '. toUpperCase ()//"Hello World" This method can also convert a Boolean value or an array to uppercase string. However, the call method is required to use String.prototype.toUpperCase.call (true)//' True ' String.prototype.toUpperCase.call ([' A ', ' B ', ' C '])/ /' A,b,c ' 8. Split ()The split method splits a string by the given rule, returning an array of separated substrings. ' A|b|c '. Split (' | ')//["A", "B", "C"]If the split rule is an empty string, the members of the returned array are each character of the original string. ' A|b|c '. Split (')//["a", "|", "b", "|", "C"]If the argument is omitted, the only member that returns the array is the original string. ' A|b|c '. Split ()//["A|b|c"]if the two parts that satisfy the split rule are next to each other (that is, there are no other characters in the middle), an empty string is returned in the array. ' a| | C '. Split (' | ')//[' A ', ', ' C ']if the part that satisfies the split rule is at the beginning or end of the string (that is, there is no other character before or after it), the first or last member of the returned array is an empty string. ' |b|c '. Split (' | ')//["", "B", "C"] ' a|b| '. Split (' | ')//["A", "B", ""]The split method can also accept the second parameter, qualifying the maximum number of members to return an array. ' A|b|c '. Split (' | ', 0)//[] ' A|b|c '. Split (' | ', 1)//["a"] ' a|b|c '. Split (' | ', 2)//["A", "B"] ' A|b|c '. Split (' | ', 3)//["a", "B", "C"] ' A|b|c '. Split (' | ', 4)//["A", "B", "C"] in the above code, the second parameter of the Split method determines the number of members that return the array.

Js-20170619-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.