Summary of common methods in js String objects (String operations)

Source: Internet
Author: User

1. The charCodeAt method returns an integer representing the Unicode encoding of characters at the specified position.
StrObj. charCodeAt (index)
Note:
Index indicates the number of characters to be processed from scratch. The valid value is a number ranging from 0 to 1 minus the string length.
If the specified position does not contain any characters, NaN is returned.
For example:
Var str = "ABC ";
Str. charCodeAt (0 );
Result: 65
2. The fromCharCode method returns a string from some Unicode strings.
String. fromCharCode ([code1 [, code2. ..])
Note:
Code1, code2. .. is the Unicode string sequence to be converted to a string. If no parameter exists, the result is a null string.
For example:
String. fromCharCode (112 );
Result:
3. The charAt method returns the characters at the specified index position. If the index value exceeds the valid range, an empty string is returned.
StrObj. charAt (index)
Note:
The index is a zero-based index of the expected characters. The valid value is the value between 0 and the string length minus one.
For example:
Var str = "ABC ";
Str. charAt (1 );
Result: B
4. The slice Method returns a string segment.
StrObj. slice (start [, end])
Note:
Start subscript specifies the part of the index in strObj starting from 0. If start is negative, it is processed as length + start. Here, length is the length of the string.
The end minor starts from 0 and the specified part of strObj ends the index. If end is negative, it is processed as length + end. Here, length is the length of the string.
For example:
012345
Var str = "ABCDEF ";
Str. slice (2, 4 );
Result: CD
5. The substring method returns the substring at the specified position in the String object.
StrObj. substring (start, end)
Note:
Start indicates the starting position of the substring. The index starts from 0.
End indicates the end position of the substring. The index starts from 0.
The substring method uses a small value in start and end as the starting point of the substring. If start or end is NaN or a negative number, replace it with 0.
For example:
012345
Var str = "ABCDEF ";
Str. substring (2, 4); // or str. substring (4, 2 );
Result: CD
6. The substr method returns a substring of the specified length starting from the specified position.
StrObj. substr (start [, length])
Note:
Start position of the substring. The index of the first character in the string is 0.
Length indicates the number of characters in the returned substring.
For example:
012345
Var str = "ABCDEF ";
Str. substr (2, 4 );
Result: CDEF
7. The indexOf method puts back the position of the substring in the String object for the first time. If no substring is found,-1 is returned.
StrObj. indexOf (substr [, startIndex])
Note:
The substring to be searched in the String object.
StartIndex indicates the index in the String object. If it is omitted, It is searched from the beginning of the string.
For example:
01234567
Var str = "ABCDECDF ";
Str. indexOf ("CD", 1); // search 123 from left to right from position 1...
Result: 2
8. The lastIndexOf method returns the final position of the String in the String object. If no substring is matched,-1 is returned.
StrObj. lastIndexOf (substr [, startindex])
Note:
The substring to be searched in the String object.
Startindex indicates the starting index position of the String object. If omitted, the search starts from the end of the string.
For example:
01234567
Var str = "ABCDECDF ";
Str. lastIndexOf ("CD", 6); // search from right to left at position 6... 456
Result: 5
9. The search method returns the position of the first string matching the content of the regular expression.
StrObj. search (reExp)
Note:
ReExp is a regular expression object that contains the regular expression mode and available flag.
For example:
Var str = "ABCDECDF ";
Str. search ("CD"); // or str. search (/CD/I );
Result: 2
10. The concat method returns a string value that contains two or more provided strings.
Str. concat ([string1 [, string2. ..])
Note:
String object or text that string1 and string2 need to connect to all other specified strings.
For example:
Var str = "ABCDEF ";
Str. concat ("ABCDEF", "ABC ");
Result: ABCDEFABCDEFABC
11. Split a string into substrings and return the result as a string array.
StrObj. split ([separator [, limit])
Note:
A separator string or regular expression object that identifies whether one or more characters are used to separate strings. If this option is ignored, a single array of elements containing the entire string is returned.
The limit value is used to limit the number of elements in the returned array.
For example:
Var str = "aa bb cc dd ee ff ";
Alert (str. split ("", 3 ));
Result:
AA, BB, CC
12. The toLowerCase method returns a string in which letters are converted to lowercase letters.
For example:
Var str = "ABCabc ";
Str. toLowerCase ();
Result: abcabc
13. The toUpperCase method returns a string in which all letters are converted to uppercase letters.
For example:
Var str = "ABCabc ";
Str. toUpperCase ();
Result: ABCABC

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.