JavaScript String object

Source: Internet
Author: User

JavaScript String object
1. describes the String object and operations on the String, such as intercepting a substring, searching for strings/characters, and converting uppercase and lowercase letters. 2. definition method: 2.1 new String (Value) constructor: returns a String object parameter whose content is Value: ① value {String}: String return Value: {String object} returns a String object with the Value: var demoStr = new String ('abc'); console. log (typeof demoStr); // => objectconsole. log (demoStr); // => abc 2.2 direct assignment (recommended) Example: var demoStr = 'abc'; console. log (typeof demoStr); // stringconsole. log (demoStr); // => abc 3. instance property 3.1 length: returns the number of characters in the string var s = 'abc'; console. log (s. length); // => 3console. log ('Happy New Year '. length); // => 4: a Chinese character is also calculated as one number of consoles. log (''. length); // => 0: Null String returns 0 4. instance method Note: The instance method of the string does not change the string itself, but only returns the result after the operation. 4.1 charAt (index): returns the character at the specified position in a string starting from 0. If a non-existent value is input, the system returns the null string parameter: ① index {int }: location index. The return value starts from 0: {string} returns the character at the specified position in a string. If a nonexistent position value is input, an example of a null string is returned: var s = 'abc'; console. log (s. charAt (1); // => B: return the character console at the position of 1. log (s); // => does not affect the original array console. log (s. charAt (5); // => '': gets a character that does not exist, and returns an empty string of 0. 4.2 charCodeAt (index ): returns the Unicode encoding parameter ① index {int}: Location index for the specified position character in a string. the return value {number} is calculated from 0 and returns the specified position in a string. Unicode encoding of a character. If a nonexistent position value is input, the NaN example is returned: var s = 'abc'; console. log (s. charCodeAt (0); // => 98: Unicode console of character B. log (s. charCodeAt (5); // => NaN: returns NaN 4.3 concat (value1, value2... valueN): connects one or more strings and returns the connected string parameters: ① value1, value2... valueN {string}: return value of one or more strings: {string} returns the connected string example: var s = 'abc'; console. log (s. concat ('D'); // => abcdconsole. log (s); // => abc: the console of the original string is not affected. log (s. Concat ('D', 'E'); // => abcde 4.4 indexOf (value, | startPosition): searches for a string or character in the instance, and return the position (counting starts from 0 ). If not found, return-1 parameter: ① value {string}: searched string ② startPosition {int} Optional: Start position of the search. The default value is 0: {int} returns the position found (counting starts from 0 ). If not found, return-1 Example: var s = 'abc'; console. log (s. indexOf ('B'); // => 1console. log (s. indexOf ('D'); // =>-1: console not found. log (s. indexOf ('B', 2); // =>-1: Search for 3rd lastIndexOf (value, | startPosition) from position 2 (4.5 characters ): search for a string or character from the back to the back in the instance, and return the found position (counting starts from 0 ). If not found, return-1 parameter: ① value {string}: string to be searched ② startPosition {int} Optional: Start position to start searching. By default, the return value is searched from the end: {int} returns the position found (counting starts from 0 ). If not found, return-1 Example: var s = 'abcabc'; console. log (s. lastIndexOf ('A'); // => 3: Search for the console from the back. log (s. lastIndexOf ('D'); // =>-1: The returned result is-1console. log (s. lastIndexOf ('A', 2); // => 0: Search for 3rd localeCompare (value) from position 2 (4.6 characters): Compare the instance with the parameter, return comparison result parameter: ① value {string}: string to be compared return value: 0: instance is greater than parameter 1: instance is equal to parameter-1: instance is smaller than parameter example: var s = 'abc'; console. log (s. localeCompare ('AB'); // => 1: The instance is larger than the parameter console. log (s. localeCompare ('abc' ); // => 0: The instance is equal to the console parameter. log (s. localeCompare ('abd'); // =>-1: The instance is smaller than the parameter. 4.7 match (regexp): Use a regular expression to match the search parameter: ① regexp {regexp }: regular Expression, eg:/\ d +/Return Value: return different results based on whether the regular expression has the attribute 'G'. If no match is found, {null} is returned }: ① if the regular expression does not contain the 'G' attribute, a match is executed and {single match} result object is returned. The object contains the following attributes: array serial number: indicates the matching result, and 0 indicates the matching text, 1 is the matching result from 1st parentheses on the right, 2 is the second parentheses, and the index attribute is as follows: indicates the input attribute of the matching text at the starting position of the matching Source: match source ② regular expression with the attribute 'G', perform global match, find all matching objects of the string, and return a {string Array}: the array element contains every matching object in the string, does not contain characters in parentheses of a regular expression. And does not provide the index and input attributes. Example: // 1. single Match var s = 'a1b2c3d4 '; var mc = s. match (/\ d +/); // => get the result of the first regular expression match if (mc! = Null) {console. log (mc. index); // => 1: The matching result is on the console at the starting position of the matching source. log (mc. input) // => a1b2c3d4: match the source console. log (mc [0]); // => 1: Obtain the matching result} // 2. global match var mcArray = s. match (/\ d +/g); // => obtain the number of all regular matches if (mcArray! = Null) {for (var I = 0, len = mcArray. length; I <len; I ++) {var mc = mcArray [I]; console. log (mc); // => 1, 2, 3, 4: Obtain matched results} // 3. matching s = 'a1b2c3d4 '; mc = s. match (/[a-z] ([1-9])/); // => get the result of the first regular expression match if (mc! = Null) {console. log (mc. index); // => 0: the matching result is on the console at the starting position of the matching source. log (mc. input) // => a1b2c3d4: match the source console. log (mc [0]); // => a1: serial number 0 indicates the matched result console. log (mc [1]); // => 1: Serial Number 1 indicates the child matching result in the first bracket} 4.8 replace (regexp, replaceStr ): replace the substring matched by the regular expression, and return the replaced string parameter: ① regexp {regexp}: regular expression. Eg:/\ d +/② replaceStr {string | function}: 1) if it is a string, it indicates the string to be replaced; the $ character in the string has a special meaning: $1, $2... $99: indicates the matching subitem of the ① parameter from left to right parentheses $ &: indicates the subitem of the entire ① Parameter Match $: Dollar sign 2) if it is a function, this function is called for each matching result. The unique parameter of the function is the matching result and a replacement result is returned. Return Value: {string} return an example of a replaced string: var oldStr = 'a1b2c3d4 '; // 1. match the regular expression with the [all] Number and replace it with ', 'comma var newStr = oldStr. replace (/\ d +/g, ','); console. log (newStr); // => a, B, c, d, // 2. replace the regular expression with the [all] number with the matching result + ', 'comma newStr = oldStr. replace (/\ d +/g, '$ &,'); console. log (newStr); // => a1, b2, c3, d4, // 3. the regular expression matches the [all] Number. Each matching result calls the function and returns the replaced result newStr = oldStr. replace (/\ d +/g, function (word) {if (word % 2 = 0) {return 'even ';} return 'Qy';}); console. log (newStr); // => a odd B even c odd d Even 4.9 search (regexp): returns the first matching location parameter of the regular expression: ① regexp {regexp }: regular Expression. Eg:/\ d +/Return Value: {int} returns the position of the first matching result. If no matching result is found, return-1 Example: console. log ('abcd '. search (/\ d +/); // =>-1: No digital console is found. log ('abc1234 '. search (/\ d +/); // => 4: The position number is 4, and the 4.10 slice (start, | end) Position of the first number is returned ): return the substring parameter from the start position of the string to the end position: ① start {int}: Index of the starting position (including characters at this position) extracted by the substring ). If the number is negative, it is calculated from the end of the string. For example,-1 indicates the last string, and-2 indicates the second to the last. ② End {int} (optional) end position index of the substring extraction (excluding characters at this position ). If the number is negative, it is calculated from the end of the string. For example,-1 indicates the last string, and-2 indicates the second to the last. If this parameter is omitted, all characters from start to end are returned. Note: The substring is extracted from left to left. If the start index is greater than the end index, an empty string is returned. Return Value: {string} returns the substring from the string start position to the previous position of end. Example: var s = 'abcdefg'; console. log (s. slice (1); // bcdefg: the end parameter is omitted, and the end position is the end console. log (s. slice (1, 3); // bc: return the sub-string console from position 1 to position 2 (previous position before end. log (s. slice (-3); // efg: returns all the characters from the last three to the end of the console. log (s. slice (-3,-1); // ef: returns all characters 4.11 split (delimiter, | arrayLength) from the last three to the second (before the end): Splits a string into an array consisting of strings according to certain delimiters and returns the parameter ① delimiter {regexp | string}: specified delimiter, which can be a regular expression or string. ② ArrayLength {int} (optional) Length of the split array. If omitted, all split substrings are returned. Note: If the separator is in the first or last character string, an empty character string is added to the returned array. Return Value: {string []} returns an array consisting of strings. Example: console. log ('a, B, c, d, e '. split (','); // => ["a", "B", "c", "d", "e"] console. log (', a, B, c, d, e ,'. split (','); // => ["", "a", "B", "c", "d", "e", ""]: the separator is at the beginning or end, and an empty string console is added. log ('a, B, c, d, e '. split (',', 3); // => ["a", "B", "c"]: returns the console of the first three substrings. log ('a1b2c3d4e '. split (/\ d/); // => ["a", "B", "c", "d", "e"]: use the number as the separator 4.12 substr (start, | wordLength): return the substring parameter from the start position of the string to the length of wordLength. Count: ① start {int}: Index of the start position extracted by the substring (including characters at this position ). If the number is negative, it is calculated from the end of the string. For example,-1 indicates the last string, and-2 indicates the second to the last. ② WordLength {int} (optional) Length of extracted characters. If this parameter is omitted, all characters from start to end are returned. Return Value: {string} return extracted string example: ar s = 'abcdefg'; onsole. log (s. substr (0); // => abcdefg: The second parameter is omitted, and the onsole from position 0 to the last character is returned. log (s. substr (0, 3); // => abc: returns the value starting from position 0 and counting 3 characters onsole. log (s. substr (2, 4); // => cdef: returns the value starting from position 2 and counting 4 characters onsole. log (s. substr (-2, 3); // fg: returns three counts starting from the second to the last string (if the length is exceeded, only the statistical characters are returned) 4.13 substring (start, | end): return the substring parameter from the string start position to the previous position of end: ① start {int }: index of the start position of the substring extraction (including the characters at this position ). The number cannot be a negative number. If it is a negative number, press 0 to process it. ② end {int} Optional: Index of the end position of the substring extraction (excluding characters at this position ). The number cannot be a negative number. If it is a negative number, the return value is 0. {string} returns the substring from the start position of the string to the end position. Example: var s = 'abcdefg'; console. log (s. substring (0); // => abcdefg: the end parameter is omitted. The console starts from position 0 and ends with the last character. log (s. substring (0, 3); // => abc: returns the character console from position No. 0 to position No. 2 (prior to ②. log (s. substring (2, 4); // => cd: returns the character console from position 2 to position 3 (prior to ②. log (s. substring (-3, 3); // abc: If the parameter is negative, the value is 0, therefore, this parameter actually returns the character 4.14 toUpperCase () from position 0 to position 3: converts the string to uppercase and returns the 4.15 toUpperCase (): convert the string to lowercase and return the 4.16 trim (): removes the white space at the beginning and end of the string and returns

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.