A detailed description of string methods in JS and the extension of the String object method

Source: Internet
Author: User
Tags first string string methods

First, the difference between slice, substr, and substring in JavaScript:

1:string.slice (start,end): a new string. Includes the string Stringobject all characters from start (including start) to end (not including end).

2:string.substring (Start,end) This is a bit special, it is first to find a small value from the start,end. Then, from the beginning of the string, intercept the position of the smaller and larger values

String, the length of the truncated string is the difference between a larger value and a smaller value.
A new string value that contains a substring of stringobject whose contents are all characters from start to stop-1, with a length of stop minus start.

3:string.substr (Start,end) This is our usual string that intercepts the specified length (end) from the specified position (start).
A new string that contains the lenght characters starting at start of Stringobject, including the character that the start refers to. If lenght is not specified, the returned string contains the start
The character to the end of the stringobject.
The methods of the String object Slice (), substring (), and substr () (not recommended) can return the specified part of the string. Slice () is more flexible than substring () because it allows negative numbers to be used as arguments. Slice () differs from substr () because it specifies a substring with a position of two characters, and substr () specifies a substring with a character position and length. ‘

Second, the method commonly used in string objects:

1. charCodeAt MethodReturns an integer that represents the Unicode of the specified position characterCoding。
Strobj.charcodeat (Index)
Description
Index is the zero-based number of characters that will be processed. Valid values are 0 to the number of string lengths minus 1.
If there are no characters at the specified location, Nan is returned.
For example:
var str = "ABC";
Str.charcodeat (0);
Results: 65
2. fromCharCode MethodReturns a string from some Unicode string.
String.fromCharCode ([Code1[,code2 ...])
Description
Code1,code2 ... is a sequence of Unicode strings to convert to a string. If there are no arguments, the result is an empty string.
For example:
String.fromCharCode (65,66,112);
Result: ABp
3. Charat MethodReturns the character at the specified index position. Returns an empty string if the index value exceeds the valid range.
Strobj.charat (Index)
Description
The zero-based index of the character that the index wants. A valid value is a value between 0 and the length of the string minus one.
For example:
var str = "ABC";
Str.charat (1);
Results: B
4. Slice methodReturns a fragment of a string.
Strobj.slice (Start[,end])
Description
The start subscript strobj The specified portion of the index starting from 0. If start is negative, it is treated as length+start, where length is the string.
End strobj Specifies a partial end index starting from 0. If end is negative, it is treated as length+end, where length is the string.
For example:
012345
var str = "ABCDEF";
Str.slice (2,4);
Result: CD
5. Substring methodReturns a substring at the specified position in a string object.
Strobj.substring (Start,end)
Description
Start indicates the starting position of the substring starting at 0.
End indicates the ending position of the substring, starting at 0.
The substring method uses the smaller values from start and end as the starting point for 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. substr MethodReturns a substring of the specified length starting at the specified position.
Strobj.substr (Start[,length])
Description
Start position of the substring that is required for start. The index of the first character in the string is 0.
Length The number of characters that should be included in the returned substring.
For example:
012345
var str = "ABCDEF";
STR.SUBSTR (2,4);
Results: Cdef
7. IndexOf MethodReturns the first occurrence of a substring position within a string object. If no substring is found, 1 is returned.
Strobj.indexof (Substr[,startindex])
Description
SUBSTR the substring to find in the string object.
startindex The integer value indicates the index at which to start the lookup within a string object. If omitted, it is searched from the beginning of the string.
For example:
01234567
var str = "ABCDECDF";
Str.indexof ("CD", 1); 1-bit left-to-right lookup 123 ...
Results: 2
8. LastIndexOf MethodReturns the position of the last occurrence of a string in a string object. If no substring is matched, 1 is returned.
Strobj.lastindexof (Substr[,startindex])
Description
SUBSTR the substring to find within the string object.
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.
For example:
01234567
var str = "ABCDECDF";
Str.lastindexof ("CD", 6); Search from right to left by 6 position ... 55W
Results: 5
9. Search method returnsThe position of the first string that matches the regular expression look-up content.
Strobj.search (REEXP)
Description
Reexp a regular Expression object that contains the regular expression pattern and the flags that are available.
For example:
var str = "ABCDECDF";
Str.search ("CD"); or Str.search (/cd/i);
Results: 2
10. Concat method returns a string valuethat contains the connection of two or more supplied strings.
Str.concat ([string1[,string2 ...])
Description
String1,string2 a String object or literal to concatenate with all other specified strings.
For example:
var str = "ABCDEF";
Str.concat ("ABCDEF", "ABC");
Results: ABCDEFABCDEFABC
11. Splitting a string into substrings, and then returns the result as an array of strings.
Strobj.split ([Separator[,limit]])
Description
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.
For example:
var str = "AA BB CC DD EE FF";
Alert (Str.split ("", 3));
Results:
Aa,bb,cc
12. toLowerCase MethodReturns a string in which the letters in the string are converted to lowercase.
For example:
var str = "ABCABC";
Str.tolowercase ();
Results: ABCABC
13. The toUpperCase method returns a string, all the letters in the string are converted to uppercase letters.
For example:
var str = "ABCABC";
Str.touppercase ();

Results: ABCABC

Although the JS string object has provided methods such as slice, replace, indexof, and substring, it is extended in the actual project application to achieve practical and convenient purposes. The comments are detailed, the nonsense is less, the code is as follows:

third, the String object method extension:
/**
* String-Formatting
*/
String.prototype.format = function () {
var args = arguments;//Gets the function to pass the parameter array for use within the Replace callback function
var regex =/\{(\d+) \}/g;//matches and captures all shapes such as: {number} string
Return This.replace (Regex,function (m,i) {//parameter = matched substring + first match + match string position + source string
return args[i];
});
}
/**
* String-Remove front and back whitespace characters
*/
String.prototype.trim = function () {
Return This.replace (/(^\s*) | ( \s*$)/g, "");
}
/**
* String-Remove the previous whitespace character
*/
String.prototype.ltrim = function () {
Return This.replace (/(^\s*)/g, "");
}
/**
* String-Remove the white space character
*/
String.prototype.rtrim = function () {
Return This.replace (/(\s*$)/g, "");
}
/**
* string-Gets the number of bytes encoded in ASCII English 1 bytes Chinese 2 bytes
*/
String.prototype.lenascii=function () {
Return This.replace (/[^\x00-\xff]/g, ' xx '). length;//converts all non-\x00-\xff characters to xx two characters, and then computes the string
}
/**
* string-Gets the number of bytes encoded in Unicode as 2 bytes per character
*/
String.prototype.lenunicode=function () {
return this.length*2;
}
PS: If you extend the method to JS to provide type objects or custom objects, you should use the prototype prototype this object property to extend it in the following way:

String.prototype.trim=function () {
//... Code slightly
};
String.prototype.ltrim=function () {
//... Code slightly
};

A detailed description of string methods in JS and the extension of the String object method

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.