String general operations in JavaScript

Source: Internet
Author: User

String Object Properties:

1. Length

To get the length of the string, it should be noted that the Chinese characters in JS also represent only one character.

var myname= "Xulinjun"; Console.log (myname.length);  8

String Object method:

1, CharAt ()

Stringobject.charat (Index)

A string that can be used to get the specified position, index is the string index value, starting from 0 to string.length-1, or an empty string if not in this range.

var a= "Hello world~"; Console.log (A.charat (4));   O

2, charCodeAt ()

Stringobject.charcodeat (Index)

Returns the Unicode encoding for the specified position character, similar to Charat (), where index is the indexed value, and the difference is the encoding of the character that returns the specified position, while the latter returns a character string.

var str = ' ABCDE '; Console.log (str.charcodeat (0));     97

3, IndexOf ()

Stringobject.slice (Start[,end])

  

This method is used to retrieve the position of the first occurrence of the specified character in the string, and he can accept two arguments, Searchvalue represents the substring to find, Fromindex represents the start of the lookup, and if omitted, it is retrieved from the starting position.

var a= "Hello world~"; Console.log (A.indexof ("O"));   4console.log (A.indexof ("O", 5))  //7console.log (A.indexof ("Lo"));  3

4, slice ();

Stringobject.slice (Start[,end])

is to intercept a paragraph in a string, start is the position index to begin to intercept, starting from 0, if start is negative, he is treated as length+start, length is the string. End is the position index that ends the interception, but the truncated string does not include the end position character, starting with 0, and if end is negative, treat it as length+end.

Here, the start position must be filled in, the end position does not have to write, does not write the word means interception to the end.

var a= "123456"; Alert (A.slice (2));      3456alert (A.slice ( -2))    //56alert (A.slice (2,4));    34

5, SUBSTRING ()

Stringobject.substring (Start,end)

Returns a substring at the specified position in the string, start is the start position, end is the ending position, all starts at 0, and the substring () method always counts as the starting position for the smaller one in start and end, and the smaller one as the ending position if they exist in Nan or negative numbers, and is replaced by 0.

var a= "123456"; alert (a.substring (2,5));   345alert (a.substring (1,6));  23456

6, substr ()

Stringobject.substr (Start[,length])

Returns a string of the specified length starting at the specified position, starting at the start position, length as the duration, but not required, if not, to the end.

var a= "123456"; Alert (A.substr (3,2));    45alert (A.SUBSTR (3));    456

7, IndexOf ()

Stringobject.indexof (Substr[,startindex])

Returns the first occurrence of a substring within a string object, if no specified substring is found, returns -1,SUBSTR to the specified substring, startindex the integer value indicates where to start, and if omitted, finds at the beginning (0).

var a= "ABCDEFG"; Alert (A.indexof ("EF", 1))   //4alert (A.indexof ("C"));    2alert (A.inenxof ("Z"))  //-1

8, LastIndexOf ()

Stringobject.lastindexof (Substr[,startindex])

Returns the position of the last occurrence of the specified string in a string object, if there is no match, returns -1,SUBSTR as the specified substring, startindex the integer value that indicates the start index position within the string object to find. If omitted, the lookup starts at the end of the string.

var a= "ABCDEFGB"; Alert (A.lastindexof ("B"));   7

  

9, concat ()

Returns a string that contains the concatenation of two or more strings.

var a= "123",    b= "456",    c= "789"; Console.log (A.concat (b,c));   123456789    

10. Split ()

Stringobject.split ([Separator[,limit]])

Splits a string into substrings and returns the result as an array of strings. Separator a string or regular expression object that identifies whether one or more characters are used when separating a string. If this option is omitted, a single element array containing the entire string is returned.
Limit this value is used to restrict the number of elements in the returned array.

var a= "AA-BB-CC"; Console.log (A.split ("-")); ["AA", "BB", "CC"]console.log (A.split ("-", 2));  ["AA", "BB"]

11, toLowerCase ()

Returns a string that is converted to lowercase letters in the string.

var a= "The weather is nice today"; Console.log (A.tolowercase ());   It's a nice day today.

12, toUpperCase ()

Returns a string in which all lowercase letters in the string are converted to uppercase letters.

var a= "The weather is nice today"; Console.log (A.touppercase ());   It's a nice day today.

  

  

  

  

  

 

  

  

  

  

String general operations in JavaScript

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.