JS String method

Source: Internet
Author: User
Tags first string

Learning a language string and array methods is a must to learn
In detail, i'll give you a little bit of reference.

String Type Basics
    1. Character literal
The String data type contains some special character literals, also known as escape sequences (characters with special Meaning)
Literal quantity Meaning
\ n Line break
\ t Tabs
\b Space
\ r Enter
\ \ or \ "or (\ ') Slash or double or single quotation marks
    1. String features
    • Immutability of strings in ECMAScript: once a string is created, their value cannot be changed
    1. Convert to String
    • ToString () Method.
      • This method returns the corresponding worthwhile string
      • Cannot convert null and undefined
      • The ToString method generally does not need to pass parameters,
      • When passing parameters (binary, 86 or 16), a string representing---any other valid binary format is represented
        Cases:
var num=10;console.log (num.tostring (+))//"a" hexadecimal 10 denotes "a";
    • String () can also be converted without knowing whether the value to be converted is null or undefined
    • String () Usage Rules
      • If there is a ToString () method, the method is called
      • Returns "null" or "undefined" if it is possible to be null or undefined (when used);
String Type Advanced
    • The string type is the wrapper type of the object of the string, which can be created by using the string constructor
var str = new string ("i am a string");
    • The methods of the Sting object can also be accessed in all basic string Values. The underlying string value represented by the object is returned through the inherited ValueOf (), tolocalstring (), and toString () methods.
Method Daquan
    • Each instance of string type has a length property---> indicates how many characters are contained in the String.
    • Character method
      • CharAt () Returns the character of the given position as a single character string.
      • charCodeAt () Returns the character encoding of the Character.
      • In ECMAScript 5, It is also defined that the specified character in a string can be obtained by using square brackets plus a numeric index: str[2];
        • Applicable ie8++
        • Ie7--will return to Undefined.
    • How to manipulate strings
      • Concat () is used to stitch one or more strings together
var stringvalue = "hello"; var result = Stringvalue.concat ("world", "!");     Alert (result);     "hello world!"   The method can be spliced with multiple characters alert (stringvalue); "hello" The method does not change the original string
        • You can stitch any number of characters to go
        • The original string is not modified
        • Use (+) to stitch strings in Real-world applications
      • method to create a new string based on a substring:
        Slice (), subStr (), subString (): when only one parameter is passed (from that position to the end of the string);
        None of the three methods will modify the original String.
var stringvalue = "hello world"; alert (stringvalue.slice (-3)); "rld" Alert (stringvalue.substring (-3)); "hello world" Alert (stringvalue.substr (-3)); "rld" Alert (stringvalue.slice (3,-4)); "lo w" Alert (stringvalue.substring (3,-4)); "hel" Alert (stringvalue.substr (3,-4)); "" empty string
        • slice ()
          • First parameter, starting position
          • second parameter , for the end position
          • parameter is negative: if there is only one argument   parameter = (parameter +str.length);
                          &NB Sp       If the second parameter is a negative value   parameter = (parameter +str.length);
        • subStr ()
        • First parameter, starting position
        • Second parameter, the number of returned characters
        • parameter is negative: if there is only one argument   parameter = (parameter +str.length);
          &N Bsp                     If the second parameter is negative, the   parameter = 0;
      • subString ();
        • Small parameter is start position, large parameter is end position
        • when parameter is negative: convert parameter to 0
    • The location method of the
  • string---> function: searches for the given string from a string and returns the position of the string (1 if not found);
var stringvalue = "hello world"; alert (stringvalue.indexof ("o", 6));   7alert (stringvalue.lastindexof ("o", 6)); 4
      • IndexOf ()
        • This method searches from the beginning of the string
        • Can receive the second parameter, from which location to start looking
      • LastIndexOf ()
        • The method searches forward from the end of the string
        • Can receive the second parameter, from which location to start looking
      • Application: A second parameter can be applied to find all strings
    • Trim () Method -----› Create a copy of a string, remove all whitespace from the predecessor and suffix, and return the result
      • ECMAScript 5 Defines the trim () method for a string
      • ie9++ Support
    • case conversion method for strings ----> It is more prudent to suggest a region-specific approach when it is not known that the code is running in that Locale.
      • toLowerCase ()
      • toLocaleLowerCase () for specific regions
      • toUpperCase ()
      • Tolocaluppercase () for specific regions
var stringvalue = "hello world"; alert (stringvalue.tolocaleuppercase ()); "HELLO world" Alert (stringvalue.touppercase ()); "HELLO world" Alert (stringvalue.tolocalelowercase ()); "hello world" Alert (stringvalue.tolowercase ()); "hello world"
    • Pattern matching method for strings
      • Match ()
        • Essentially the exec () method that calls RegExp is the same
        • Match receives only one parameter (regular expression or RegExp Object)
        • return value: an array
        • If no match is found, returns-1;
var text = "cat, bat, sat, fat"; var pattern =/.at/;//and pattern.exec (text) results in the same var matches = Text.match (pattern); The method returns an array of alert (matches.index); 0alert (matches[0]); "cat" Alert (pattern.lastindex); 0
      • Search ()
        • Arguments: a string or a regular expression made by a RegExp object
        • Return value: returns the index of the first occurrence in a string if no return-1 is found
      • To simplify the operation of the replacement string: ECMAScript provides replace () guard
        • Replace () method
          • Parameters: the first argument, the RegExp object, or a string (the string is not converted to a regular Expression)
            The second argument, a string, or a function
            If the first argument is a string, then only the first string is replaced,
            If you want to replace all strings, the only way is to provide a regular expression, and to make the global (g) flag
var text = "cat, bat, sat, fat"; var result = Text.replace ("at", "ond"); alert (result);    "cond, bat, sat, fat" result = Text.replace (/at/g, "ond"); Add global (g) flag alert (result); "cond, bond, sond, fond"
        • Split () splits a string into multiple substrings based on the specified delimiter and places the result in an array
          • First argument: can be a string or a RegExp object
          • You can also receive the second parameter---specify the size of the array
var colortext = "red,blue,green,yellow"; var colors1 = Colortext.split (","); ["red", "blue", "green", "yellow"]var colors2 = colortext.split (",", 2); ["red", "blue"]var colors3 = Colortext.split (/[^\,]+/); //["", ",", ",", ",", ""]
          • Various browsers have different ways of handling incoming RegExp objects Learn more about reference JavaScript split bugs:fixed! (http://blog.stevenlevithan.com/archives/ Cross-browser-split) Steven Levithan
    • Localecompare () method
      • Function: compares two strings and returns the following return values
        • If the string precedes the passed argument string in the Alphabet: returns a negative number (typically-1),
          Inverse: returns a positive number (typically 1)
        • If two strings are the same: return 0
var stringvalue = "yellow"; Alert (stringvalue.localecompare ("brick"));    1alert (stringvalue.localecompare ("yellow"));      0alert (stringvalue.localecompare ("zoo")); -1
    • Forcharcode () method
      • A static method of the string constructor itself----> receives one or more string encodings and then converts them into strings
Alert (string.fromcharcode (104, 101, 108, 108, 111)); "hello"
      • HTML method to turn it into an HTML Tag----now stop using

JS String 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.