SoFunction
Updated on 2025-03-03

JS string operation summary (must read)

Character Method

<!DOCTYPE html> 
<html lang="en"> 
  <head> 
    <meta charset="utf-8"> 
    <title>Character Method</title> 
  </head> 
  <body> 
  <script type="text/javascript"> 
  /*
   Both the charAt method and the charCodeAt method receive a parameter based on the character position of 0.
   The charAt method returns the character at the given position as a single character string.
   What the charCodeAt method gets is not a character but a character encoding
    */ 
    var str="hello world"; 
    ((1));//e 
    ((1));//101 
    // You can also use square brackets and numeric indexes to access specific characters in the string    (str[1]);//e 
  </script> 
  </body> 
</html> 

String operation method
concat method

<!DOCTYPE html> 
<html lang="en"> 
  <head> 
    <meta charset="utf-8"> 
    <title>concatmethod</title> 
  </head> 
  <body> 
  <script type="text/javascript"> 
    var str="hello "; 
    var res=("world"); 
    (res);//hello world 
    (str);//hello This means that the value of the original string has not changed    var res1=("nihao","!"); 
    (res1);//hello nihao! Explain that the concat method can receive any multiple parameters    //Although the concat method is specially used to splice strings, the most used thing we use in practice is to add operator + because it is simple and convenient  </script> 
  </body> 
</html> 

slice method, substring method, substr method

<!DOCTYPE html> 
<html lang="en"> 
  <head> 
    <meta charset="utf-8"> 
    <title>String operation method</title> 
  </head> 
  <body> 
  <script type="text/javascript"> 
    /*
     slice method: The first parameter specifies the start position of the substring, and the second parameter specifies the position after the last character of the substring
     substring method: The first parameter specifies the start position of the substring, and the second parameter specifies the position after the last character of the substring
     substr method: The first parameter specifies the start position of the substring, and the second parameter indicates the number of characters returned
     These three methods will return a substring of the manipulated string, and both receive one or two parameters
     If no second argument is passed to these methods, the length of the string is used as the end position.  These methods also do not modify the string itself, but only return a basic type of string value
      */ 
    var str="hello world"; 
    ((3));//lo world 
    ((3));//lo world 
    ((3));//lo world 
    ((3,7));//lo w 7 represents the position after the last character of the substring. Simple understanding is to include the head but not the tail    ((3,7));//lo w 
    ((3,7));//lo world 7 means return 7 characters 
    ((3,-4));//lo w -4+11=7 indicates the position after the last character of the substring. Simple understanding is to include the head but not the tail.    ((3,-4));//hel will be converted to ((3,0));    //In addition, since this method will use a smaller number as the start position and a larger number as the end position, it is equivalent to calling ((0,3));    ((3,-4));//""Empty string    ((3,0)); 
  </script> 
  </body> 
</html> 

String position method

<!DOCTYPE html> 
<html lang="en"> 
  <head> 
    <meta charset="utf-8"> 
    <title>String position method</title> 
  </head> 
  <body> 
  <script type="text/javascript"> 
    /*
     Both indexOf method and lastIndexOf method search for a given substring from a string, and then return the position of the substring. If not found, return -1
     indexOf method is to search for substring from the beginning of the string backwards, and lastIndexOf method is exactly the opposite
     Both methods can receive two parameters: the substring to be found and the location to be found
      */ 
    var str="hello world"; 
    (("o"));//4 
    (("o"));//7 
    (("o",6));//7 
    (("o",6));//4 
  </script> 
  </body> 
</html> 

Trim method

<!DOCTYPE html> 
<html lang="en"> 
  <head> 
    <meta charset="utf-8"> 
    <title>trimmethod</title> 
  </head> 
  <body> 
  <script type="text/javascript"> 
    /*
     Trim method is used to delete spaces before and after a string
      */ 
    var str="   hello world   "; 
    ('('+()+')');//(hello world) 
    ('('+str+')');//(   hello world   ) 
  </script> 
  </body> 
</html> 

String case conversion method

<!DOCTYPE html> 
<html lang="en"> 
  <head> 
    <meta charset="utf-8"> 
    <title>Case conversion</title> 
  </head> 
  <body> 
  <script type="text/javascript"> 
    var str="HELLO world"; 
    (());//hello world 
    (());//HELLO WORLD 
  </script> 
  </body> 
</html> 

String pattern matching method

<!DOCTYPE html> 
<html lang="en"> 
  <head> 
    <meta charset="utf-8"> 
    <title>String pattern matching</title> 
  </head> 
  <body> 
  <script type="text/javascript"> 
  /*
   match method: accepts only one parameter, a regular expression specified by a string or RegExp object
   Search method: Accept only one parameter, a regular expression specified by a string or RegExp object
   The search method returns the index of the first match in the string. If there is no match, return -1
    */ 
  var str="cat,bat,sat,fat"; 
  var pattern=/.at/; 
  var matches=(pattern); 
  ();//0 
  (matches[0]);//cat 
  ();//0 
  //lastIndex indicates the character position of the next match, starting from 0  var pos=(/at/); 
  (pos);//1 1 indicates the first occurrence of the at string in the original string  </script> 
  </body> 
</html> 

Replace method

<!DOCTYPE html> 
<html lang="en"> 
  <head> 
    <meta charset="utf-8"> 
    <title>replacemethod</title> 
  </head> 
  <body> 
  <script type="text/javascript"> 
    var str="cat,bat,sat,fat"; 
    var res=("at","one");//The first parameter is a string, so it will only replace the first substring    (res);//cone,bat,sat,fat 
 
    var res1=(/at/g,"one");//The first parameter is a regular expression, so all substrings will be replaced    (res1);//cone,bone,sone,fone 
  </script> 
  </body> 
</html> 

split method

<!DOCTYPE html> 
<html lang="en"> 
  <head> 
    <meta charset="utf-8"> 
    <title>splitmethod</title> 
  </head> 
  <body> 
  <script type="text/javascript"> 
  /*
   Split method is to split a string into a string array based on the specified characters
   When the specified character is an empty string, the entire string will be separated
    */ 
    var str="red,blue,green,yellow"; 
    ((","));//["red", "blue", "green", "yellow"] 
    ((",",2));//["red", "blue"] The second parameter is used to limit the array size    ((/[^\,]+/));// ["", ",", ",", ",", ""] 
    //The first and last items are empty strings because the separator specified by the regular expression appears at the beginning of the substring, i.e. "red" and "yellow"    //[^...] Any character not in square brackets. As long as it is not a comma, it is a separator.  </script> 
  </body> 
</html> 

localeCompare method

<!DOCTYPE html> 
<html lang="en"> 
  <head> 
    <meta charset="utf-8"> 
    <title>localeComparemethod</title> 
  </head> 
  <body> 
  <script type="text/javascript"> 
    /*
     This method is used to compare two strings
     1. If the string should be ranked before the string parameter in the alphabet, a negative number will be returned
     1. If the string is equal to the string parameter, return 0
     1. If the string should be placed after the string parameter in the alphabet, a positive number will be returned.
      */ 
    var str="yellow"; 
    (("brick"));//1 
    (("yellow"));//0 
    (("zoo"));//-1 
  </script> 
  </body> 
</html> 

fromCharCode method

<!DOCTYPE html> 
<html lang="en"> 
  <head> 
    <meta charset="utf-8"> 
    <title>fromCharCodemethod</title> 
  </head> 
  <body> 
  <script type="text/javascript"> 
    /*
     fromCharCode method is to receive one or more character encodings and then convert them into strings
     fromCharCode method is a static method of the String constructor
      */ 
    ((104,101,108,108,111));//hello 
  </script> 
  </body> 
</html> 

Find the locations where the matching string is located

<!DOCTYPE html> 
<html lang="en"> 
  <head> 
    <meta charset="utf-8"> 
    <title>String matching</title> 
  </head> 
  <body> 
  <script type="text/javascript"> 
  /* Find the location where the matching string is located*/ 
    var str="asadajhjkadaaasdasdasdasd"; 
    var position=[]; 
    var pos=("d"); 
    while(pos>-1){ 
      (pos); 
      pos=("d",pos+1); 
    } 
    (position);//[3, 10, 15, 18, 21, 24] 
  </script> 
  </body> 
</html> 

Deduplication of string

<!DOCTYPE html> 
<html lang="en"> 
  <head> 
    <meta charset="utf-8"> 
    <title>Deduplication of string</title> 
  </head> 
  <body> 
  <script type="text/javascript"> 
  //() The operation is the opposite of the operation performed  //split() method is used to split a string into a string array.  //join method is used to concatenate a string array into a string  //If an empty string ("") is used as a separator, then each character in the stringObject will be split.    var str="aahhgggsssjjj";//There are no characters that can be separated here, so you need to use an empty string as a separator    function unique(msg){ 
      var res=[]; 
      var arr=(""); 
      //(arr); 
      for(var i=0;i<;i++){ 
        if((arr[i])==-1){ 
          (arr[i]); 
        } 
      } 
      return (""); 
    } 
    (unique(str));//ahgsj 
  </script> 
  </body> 
</html> 

Determine the number of characters in a string

<!DOCTYPE html> 
<html lang="en"> 
  <head> 
    <meta charset="utf-8"> 
    <title>String operation</title> 
  </head> 
  <body> 
  <script type="text/javascript"> 
  /*
   1. First implement string deduplication
   2. Then use a for loop operation on the deduplicated array, and compare it with each value in the original array. If it is equal, count++. After the loop, save the count in the sum array, and then reset count to 0.
   3. In this way, the number of times the elements in the array appear in the original array is one by one and the elements in the sum array.
    */ 
    var str="aacccbbeeeddd"; 
    var sum=[]; 
    var res=[]; 
    var count=0; 
    var arr=(""); 
    for(var i=0;i<;i++){ 
      if((arr[i])==-1){ 
        (arr[i]); 
      } 
    } 
    for(var i=0;i<;i++){ 
      for(var j=0;j<;j++){ 
        if(arr[j]==res[i]){ 
          count++; 
        } 
      } 
      (count); 
      count=0; 
    } 
    (res);//["a", "c", "b", "e", "d"] 
    for(var i=0;i<;i++){ 
      var str=(sum[i]%2==0)?"even":"odd number"; 
      (res[i]+"It appears"+sum[i]+"Second-rate"); 
      (res[i]+"It appears"+str+"Second-rate"); 
    } 
  </script> 
  </body> 
</html> 

Ali Test - String Operation

<script type="text/javascript"> 
  var str = ""; 
  var res = ("").reverse().join("").replace('oat',''); 
  (res);// 
</script> 

The above is the full content of the js string operation summary (must-read article) brought to you by the editor. I hope everyone supports me~