js string algorithm

General methods of strings

1.concat stitching string

    var a="123";
    var b="456";
    console.log(a.concat(b));            //123456
    console.log(a.concat(b).concat(a));  //123456123

2.indexOf finds characters from left to right and returns an index or -1

Note: No matter which direction you look for a character, the index of the character will not change

Although indexOf and search both return indexes based on characters, indexOf has two parameters and one parameter, and search has only one case

Single parameter, find characters directly

    left="qweuhgq";
    console.log(left.indexOf("q"));   //0
    console.log(left.indexOf("m"));   //-1

Two parameters (parameter, startindex)

  left="qweuhgq";
  console.log(left.indexOf("w",2));    //-1
  console.log(left.indexOf("we",1));   //1
  console.log(left.indexOf("wq",0));   //-1

3.lastindexOf finds characters from right to left

    left="qweuhgq";
    console.log(left.lastIndexOf("w"));     //1
    console.log(left.lastIndexOf("u",2));   //-1

4. CharAts return corresponding characters based on the index

Compared to search,indexOf, search,indexOf finds and returns the corresponding index based on the character

charCodeAt() returns the corresponding ASCII code value

String.fromCharCode() converts the corresponding ASCII code value to a character

    code="1a4578321";
    console.log(code.charAt('1'));          //a
    console.log(code.charCodeAt("1"));      //97
    console.log(String.fromCharCode('97')); //a

5.substr (where to intercept, length of intercept)

Both intercepts have no effect on the original string, returning the intercepted character

substring (index, index) Two indexes don't matter where they start or end

    jiequ="gduakdnakj";
    console.log(jiequ.substr(1, 2));     //du
    console.log(jiequ);                  //gduakdnakj
    console.log(jiequ.substring(2, 1));  //d
 console.log(jiequ.substring(1, 2));  //d
    console.log(jiequ);                  //gduakdnakj

6.search finds the character and returns the current character index, if not -1

Note: If there are multiple identical characters, only the first character index will be returned

    find="aggjklmhyv";
    console.log(find.search("g"));   //1

7.replace character replacement, return value is the string after replacement, has no effect on the original string

Note: Only the first character can be replaced

    rep="12346782";
    console.log(rep.replace("2", "0"));  //10346782
    console.log(rep);                   //12346782

8.slice intercepts strings (start, end)

English Meaning: v. Cut

Returns the intercepted character without affecting the original string
    jiequ2="gduakdnakj";
    console.log(jiequ2.slice(1, 2));    //d
    console.log(jiequ2.slice(2, 1));   //Output null
    console.log(jiequ2);               //gduakdnakj

9.split converts strings to arrays

English Meaning: v. Separate
join converts an array to a string

Find the characters in parentheses and separate the characters on either side of the character
    strr="12,345. 6";
    console.log(strr.split());       //["12,345.6']Converted directly to an array
    console.log(strr.split(" "));    //["12,345. 6"]
    console.log(strr.split(""));   	 //["1", "2", ",", "3", "4", "5", ". ", "6"]
    console.log(strr.split(". "));   // ["12,345", "6"]
    s="1+2+3+4+5";
    console.log(s.split("+"));       //["1", "2", "3", "4", "5"]

10.length length

Length is generally used in loops

11.toUpperCase to LowerCase case case conversion

    str="abcdefg";
    console.log(str.toUpperCase());     //ABCDEFG
    str2="ABCDE";
    console.log(str2.toLowerCase());    //abcde

Keywords: ascii

Added by akano on Mon, 12 Aug 2019 05:39:29 +0300