Summarize the common or uncommon string processing methods.

Source: Internet
Author: User

 

What are the commonly used string processing methods? Commonly used and not commonly used are available. It is necessary to summarize

1. charat: returns the characters at the specified position;

<SCRIPT type = "text/JavaScript"> return value: "E" Var STR = "Hello world! "Document. Write (Str. charat (1) </SCRIPT>
2. charcodeat: return the Unicode encoding of characters at the specified position. The returned value is an integer between 0 and 65535. Charcodeat () is similar to the charat () method, except that the former returns the encoding of the characters at the specified position, and the latter returns the character substrings.

<SCRIPT type = "text/JavaScript"> return value: 101
VaR STR = "Hello world! "
Document. Write (Str. charcodeat (1 ))
</SCRIPT>
3. Concat: Used to merge and join a string (array)
VaR A = '20160901'; A. Concat ('20160901'); Return Value: '20160901'
4. indexof: searches for the position in a character string.
VaR A = 'hello'; A. indexof ('l'); Return Value: 2
5. lastindexof: The method returns the last position of a specified string value and searches forward from the specified position in the string.
VaR A = 'hello'; A. lastindexof ('l'); Return Value: 3
6. localecompare: Compares two strings in a specific local order.
This method seems to have been rarely used and does not know its application scenarios for the moment. The return value is interpreted as follows: the number of the comparison result. If stringobject is smaller than target, localecompare () returns a number smaller than 0. If the value of stringobject is greater than target, the return value of this method is greater than 0. If the two strings are equal or there is no difference according to the local sorting rules, this method returns 0.

Note: When the <and> operators are applied to strings, they only use Unicode encoding to compare strings without considering local sorting rules. The order generated in this method is not necessarily correct. For example, in Spanish, the character "ch" is usually sorted as a character between the letter "C" and "D. The localecompare () method provides the method for comparing strings, taking into account the default local sorting rules. The ecmascript standard does not specify how to perform specific local comparison operations. It only specifies that the function uses the sorting rules provided by the underlying operating system.

7. Match: The method can be used to search for a specified value in a string or to find a match between one or more regular expressions.

This method is similar to indexof () and lastindexof (), but it returns the specified value rather than the position of the string.

<SCRIPT type = "text/JavaScript"> return value: 1, 2, 3 var STR = "1 plus 2 equal 3" document. Write (str.match(/\d+/g))

</SCRIPT>
8. Search: This method is used to retrieve the specified substring in a string or a substring that matches a regular expression.
<SCRIPT type = "text/JavaScript"> return value: 6 var STR = "Visit w3school! "Document. Write (Str. Search (/w3school/) </SCRIPT>
9. Slice: You can return the selected elements from an existing array.
VaR A = 'str1231'; A. Slice (2, 3); Return Value: 'R'
10. Split: splits strings into arrays;
VaR A = 'H e l o';. split ('') Return Value: [" H "," E "," L "," L "," O "]
11, substr: can be extracted from the stringStartA specified number of characters starting with a subscript.
    
Start Required. The starting subscript of the substring to be extracted. It must be a numerical value. If it is a negative number, this parameter declares the position starting from the end of the string. That is to say,-1 refers to the last character in the string,-2 refers to the second to last character, and so on.
Length Optional. The number of characters in the substring. It must be a numerical value. If this parameter is omittedStringobjectThe start position to the end of the string.

VaR S = 'hello'; S. substr (2, 1); Return Value: 'l'

12. substring: This method is used to extract characters that are intermediate between two specified subscripts.

VaR S = 'hello'; S. substr (0, 1); Return Value: 'H'

Parameter description
Start is required. A non-negative integer that specifies the position of the first character of the substring to be extracted in the stringobject.
Optional. A non-negative integer is 1 more than the position of the last character of the substring to be extracted in the stringobject. If this parameter is omitted, the returned substring ends at the end of the string.

13. tolocalelowercase and tolowercase: used to convert a string to lowercase.
Different from tolowercase (), the tolocalelowercase () method converts the string to lowercase in local mode. Only a few languages (such as Turkish) have local-specific case ing. The return values of all methods are usually the same as those of tolowercase.
14: tolocaleuppercase, touppercase: used to convert a string to uppercase
15. tostring: converts a logical value to a string and returns the result.
16. valueof: returns the original value of a Boolean object. If the object that calls this method is not a Boolean object, an exception typeerror is thrown.
17. Trim: removes the null characters on both sides of the string.
   
     
 

    

  

     

    

  

  

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.