Home  >  Article  >  Web Front-end  >  Summary of commonly used methods in js String objects (string operations)_javascript skills

Summary of commonly used methods in js String objects (string operations)_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:56:451048browse
1. The charCodeAt method returns an integer representing the Unicode encoding of the character at the specified position.
strObj.charCodeAt(index)
Description:
index will be processed as the zero-based number of the character. Valid values ​​are 0 to the length of the string minus 1.
If there is no character at the specified position, NaN will be returned.
For example:
var str = "ABC";
str.charCodeAt(0);
Result: 65
2. The fromCharCode method returns a character from some Unicode strings string.
String.fromCharCode([code1[,code2...]])
Explanation:
code1, code2... are the Unicode string sequences to be converted to strings. If there are no parameters, the result is an empty string.
For example:
String.fromCharCode(65,66,112);
Result: ABp
3. The charAt method returns the character at the specified index position. If the index value is outside the valid range, an empty string is returned.
strObj.charAt(index)
Description:
index The zero-based index of the desired character. Valid values ​​are between 0 and the length of the string minus one.
For example:
var str = "ABC";
str.charAt(1);
Result: B
4. The slice method returns a fragment of a string.
strObj.slice(start[,end])
Explanation:
The specified part of strObj whose start subscript starts from 0 is actually the index. If start is negative, treat it as length start, where length is the length of the string.
end subscript specifies the partial end index of strObj starting from 0. If end is negative, treat it as length end, where length is the length of the string.
For example:
012345
var str = "ABCDEF";
str.slice(2,4);
Result: CD
5. The substring method returns String The substring at the specified position in the object.
strObj.substring(start,end)
Description:
start specifies the starting position of the substring, and the index starts from 0.
end specifies the end position of the substring, and the index starts from 0.
The substring method uses the smaller value of start and end as the starting point of the substring. If start or end is NaN or negative, 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])
Description:
start The starting position of the required substring. The first character in the string has index 0.
length The number of characters that should be included in the returned substring.
For example:
012345
var str = "ABCDEF";
str.substr(2,4);
Result: CDEF
7. The indexOf method puts back the String The position of the first occurrence of the substring within the object. If the substring is not found, -1 is returned.
strObj.indexOf(substr[,startIndex])
Description:
substr The substring to be found in the String object.
startIndex This integer value indicates the index within the String object to start searching. If omitted, the search is 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 last position of the string in the String object. If no substring is matched, -1 is returned.
strObj.lastIndexOf(substr[,startindex])
Description:
substr is the substring to be found in the String object.
startindex This integer value indicates the starting index position of the search within the String object. If omitted, the search starts at 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 that matches the regular expression search content.
strObj.search(reExp)
Description:
reExp is a regular expression object containing the regular expression pattern and available flags.
For example:
var str = "ABCDECDF";
str.search("CD"); // or str.search(/CD/i);
Result: 2
10. The concat method returns a string value, which contains the connection of two or more provided strings.
str.concat([string1[,string2...]])
Description:
string1, string2 is the String object or text to be concatenated with all other specified strings.
For example:
var str = "ABCDEF";
str.concat("ABCDEF","ABC");
Result: ABCDEFABCDEFABC
11. Split a string as a substring and return the result as a string array.
strObj.split([separator[,limit]])
Description:
separator string or regular expression object, which identifies whether one or more characters are used to separate the string . If this option is omitted, a single-element array containing the entire string is returned.
limit This 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, and the letters in the string are converted to lowercase.
For example:
var str = "ABCabc";
str.toLowerCase();
Result: abcabc
13. The toUpperCase method returns a string, the character All letters in the string are converted to uppercase letters.
For example:
var str = "ABCabc";
str.toUpperCase();
Result: ABCABC
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn