Common Operations on strings in javascript

Source: Internet
Author: User
String operations are very frequent and important in js. I can remember it clearly after reading the book, but I don't need it for a while, so I will forget about it. It is hard to remember a poor memory... Today, we will sort out some common operations on strings to help you better understand them. In the future, SyntaxHighlighter. all (); string operations are very frequent and important in js. I can remember it clearly after reading the book, but I don't need it for a while, so I will forget about it. It is hard to remember a poor memory... Today, we will sort out some common operations on strings to help you better understand them. You can also view it in the independent blog: String common operations in javascript summary String object attribute (1) length is a very common attribute in strings. Its function is to obtain the length of a String. Of course, it should be noted that each Chinese Character in js also represents only one character, which may be different from other languages. Var str = 'abc'; console. log (str. length); (2) the prototype attribute prototype is often used in object-oriented programming. It is used to add attributes or methods to an object, and the added methods or attributes are shared across all instances. Therefore, js built-in objects are also often used to expand. The following Code adds a method to remove spaces on both sides to the String: String. prototype. trim = function () {return this. replace (/^ \ s * | \ s * $/g, '');} String object method 1. obtain the class method (1) charAt () stringObject. the charAt (index) charAt () method can be used to obtain the string at the specified position. index is the string index value, starting from 0 to string. leng-1. If it is not in this range, an empty string is returned. For example, var str = 'abcde'; console. log (str. charAt (2); // return to cconsole. log (str. charAt (8); // returns an empty string (2) charCodeAt () stringObject. the charCodeAt (index) charCodeAt () method returns the Unicode encoding of characters at the specified position. The charCodeAt () method is similar to the charAt () method. You need to input an index value as a parameter. The difference is that the former returns the encoding of the character at the specified position, and the latter returns the character substring. Var str = 'abcde'; console. log (str. charCodeAt (0); // return 97 (3) fromCharCode () String. fromCharCode (numX, numX ,..., NumX) fromCharCode () accepts one or more Unicode values and returns a string. In addition, this method is a static String method. Each character in a String is specified by a separate numeric Unicode encoding. String. fromCharCode (97, 98, 99,100,101) // return abcde2. lookup class method (1) indexOf () stringObject. indexOf (searchvalue, fromindex) indexOf () is used to retrieve the position where the specified string value first appears in the string. It can receive two parameters. searchvalue indicates the substring to be searched, fromindex indicates the start position of the search, and if it is omitted, retrieval is performed from the start position. Var str = 'abcdeabcde'; console. log (str. indexOf ('A'); // return 0console. log (str. indexOf ('A', 3); // return 5console. log (str. indexOf ('bc'); // returns 1 (2) lastIndexOf () method stringObject. the lastIndexOf (searchvalue, fromindex) lastIndexOf () syntax is similar to indexOf (). It returns the last position of a specified substring value, and its retrieval order is from the back to the front. Var str = 'abcdeabcde'; console. log (str. lastIndexOf ('A'); // return 5console. log (str. lastIndexOf ('A', 3); // return 0 to retrieve console from index 3. log (str. lastIndexOf ('bc'); // returns the 6 (3) search () method stringObject. search (substr) stringObject. the search (regexp) search () method is used to retrieve the specified substring in a string or a substring that matches a regular expression. It returns the starting position of the first matched substring. If no matching substring exists,-1 is returned. Var str = 'abcdef'; console. log (str. search ('C'); // returns 2console. log (str. search ('D'); // return-1console. log (str. search (/d/I); // returns the 3 (4) match () method stringObject. match (substr) stringObject. the match (regexp) match () method can be used to retrieve the specified value in a string, or to find matching of one or more regular expressions. If the input parameter is a substring or a regular expression that does not perform a global match, the match () method executes a match from the starting position. If no match is found, null is returned. Otherwise, an array is returned. The 0th elements in the array store the matching text. In addition, the returned array also contains two object attributes: index and input, the index that matches the start character of the text and the reference of the stringObject (that is, the original string ). Var str = '1a2b3c4d5e '; console. log (str. match ('H'); // return nullconsole. log (str. match ('B'); // return ["B", index: 3, input: "1a2b3c4d5e"] console. log (str. match (/B/); // return ["B", index: 3, input: "1a2b3c4d5e"] If the input parameter is a regular expression with global matching, then match () is performed multiple times from the starting position until the end. If no matching result is found, null is returned. Otherwise, an array is returned, which stores all the required substrings without the index and input attributes. Var str = '1a2b3c4d5e '; console. log (str. match (/h/g); // return nullconsole. log (str. match (/\ d/g); // Returns ["1", "2", "3", "4", "5"] 3. truncation method (1) substring () stringObject. substring (start, end) substring () is the most common string truncation method. It can receive two parameters (the parameter cannot be a negative value ), it is the start position and end position to be intercepted respectively. It returns a new string whose content is all characters from start to end-1. If the end parameter is omitted, the end parameter is truncated from the start position until the end. Var str = 'abcdefg'; console. log (str. substring (1, 4); // return bcdconsole. log (str. substring (1); // return bcdefgconsole. log (str. substring (-1); // return abcdefg. 0 (2) slice () stringObject. the slice (start, end) slice () method is similar to the substring () method. The two parameters it passes in correspond to the start position and end position respectively. The difference is that the slice () parameter can be a negative value. If the parameter is a negative value, this parameter specifies the position starting from the end of the string. That is,-1 refers to the last character of the string. Var str = 'abcdefg'; console. log (str. slice (1, 4); // return to the bcdconsole. log (str. slice (-3,-1); // return to efconsole. log (str. slice (1,-1); // return bcdefconsole. log (str. slice (-1,-3); // returns an empty string. If the input parameter is incorrect, null (3) substr () stringObject is returned. the substr (start, length) substr () method extracts a specified number of characters starting with the start subscript from a string. The return value is a string that contains length characters starting from the start (including the characters indicated by start) of the stringObject. If length is not specified, the returned string contains characters from start to the end of stringObject. In addition, if 'start' is a negative number, it indicates starting from the end of the string. Var str = 'abcdefg'; console. log (str. substr (1, 3) // return to the bcdconsole. log (str. substr (2) // return cdefgconsole. log (str. substr (-2, 4) // returns fg. If the target length is large, the actual truncation length prevails. 4. other methods (1) replace () method stringObject. the replace (regexp/substr, replacement) replace () method is used to replace strings. It can receive two parameters. The former is the replaced substring (which can be a regular expression ), the latter is the text to be replaced. If the first parameter is a substring or a regular expression that does not match globally, the replace () method will only replace (that is, replace the first ), returns the result string after a replacement. Var str = 'abcdeabcde'; console. log (str. replace ('A', 'A'); console. log (str. replace (/a/, 'A'); If the first parameter is passed in A globally matched regular expression, then replace () the sub-string that meets the condition is replaced multiple times, and the result string that has been replaced multiple times is returned. Var str = 'abcdeabcde'; console. log (str. replace (/a/g, 'A'); // return AbcdeAbcdeABCDEconsole. log (str. replace (/a/gi, '$'); // returns $ bcde $ BCDE (2) split () method stringObject. the split (separator, howator) split () method is used to split a string into a string array. The first parameter separator indicates the split position (reference), and the second parameter howator indicates the maximum length allowed for the returned array (not set in general ). Var str = 'a | B | c | d | E'; console. log (str. split ('|'); // return ["a", "B", "c", "d", "e"] console. log (str. split ('|', 3); // return ["a", "B", "c"] console. log (str. split (''); // Returns [" a "," | "," B "," | "," c "," | "," d ", "|", "e"] You can also use regular expressions to split var str = 'a1b2c3d4e '; console. log (str. split (/\ d/); // return ["a", "B", "c", "d", "e"] (3) toLowerCase () and toUpperCase () stringObject. toLowerCase () stringObject. toUpperCase () toLowerCase () method You can convert uppercase letters in a string to lowercase letters. The toUpperCase () method can convert lowercase letters in a string to uppercase letters. Var str = 'javascript '; console. log (str. toLowerCase (); // return javascriptconsole. log (str. toUpperCase (); // return JavaScript
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.