SoFunction
Updated on 2025-03-01

JavaScript entry-level string object [must-read for beginners]

1. String object

A String object is used to process text (string).

2. Constructor function

new String(value)//Constructor
function String(value)//Conversion function

3. Attributes

length number of characters in this string

      var str = new String("abcdefg");
      ();   //Output 7

4. Method

1. chatAt() takes out a character at the specified position in a string.

      var str = new String("abcdefg");
      ((1));   //Output b

2. chatCodeAt() returns the encoding of the characters at the specified position in a string.

      var str = new String("abcdefg");
      ((1));   //Output 98

3. concat() concatenates one or more values ​​into a string.

      var str = new String("abcdefg");
      var str1 = "hijk";
      ((str1));   //Output abcdefghijk

4. indexOf() finds the location of a character or string in the specified string. If no searches are found, return -1

Syntax: indexOf(str) str: substring or character

indexOf(str,start)str: substring or character. start: Specify the start location of the search

      var str = new String("abccba");
      (('b'));   //Output 1  (("bc"));   //Output 1

Use this method to implement the Contains effect and determine whether a string contains another string:

<script type="text/javascript">
     = function () {
      var str1 = "Liu Bei";
      var str2 = "Liu Bei is a great man!";
      alert((str1)); //Output 0 appears      if ((str1) > -1) {
        alert("Include!");
      }
      else {
        alert("Not included!");
      }
    }
  </script>

5. LastIndexOf() looks for the position of a character or string backwards (inverted) in the specified string. If no searches are found, return -1

Syntax: lastIndexOf(str) str: substring or character

lastIndexOf(str,start)str: a substring or character. start: Specify the start location of the search

      var str = new String("abccba");
      (('b'));   //Output 4

6. localeCompare() compares strings using locally defined order.

      var str = "abccba";
      (("bc"));//Output -1

7. match() uses regular expressions to perform pattern matching.

8. replace() uses regular expressions to perform search and replace operations.

      var str = "abccba";
      (("b","-"));//Output a-ccba

9. Search() finds a string that matches a regular expression in a string.

      var str = "abccba";
      (("b"));  //Output 1

10. slice() returns a slice or string of the string. If the parameter is a negative number, it means the number from behind to front. Do not change the original string.

      var str = "abcdefg";
      ((2) + "<br/>"); //Output cdefg      (str);          //OutputabcdefgYou can see that the original string has not changed。

11. split() is broken with the specified delimiter string or regular expression, and returns the string array.

      var str = "abcdefg";
      var arr = ("d");
      (());          //Outputabc,efg

12. substr() extracts a substring of a string, a variant of substring(). Deprecated.

13. substring() Extracts a substring of a string.

Syntax: substring(start,end) starts from start and ends, including start but not end. Do not change the original string.

      var str = "12345678";
      ((1,4));//Output 234

14. toLowerCase() returns a lowercase copy of the specified string.

      var str = "abcDEF";
      (());  //Output abcdef

15. toString() returns the original string value.

      var str = "abcDEF";
      (());  //Output abcDEF

16. toUpperCase() returns a capital copy of the specified string.

      var str = "abcDEF";
      (());  //Output ABCDEF

17. trim() returns a copy of the specified string that is blank before and after removal.

      var str = "  abcDEF  ";
      ("11" + () + "11" + "<br/>");  //Output 11abcDEF11      ("11" + str + "11");  //Output 11 abcDEF 11

18. valueOf() returns the original string value.

      var str = "abcDEF";
      (());  //Output abcDEF

The above is all the content of this article. I hope it will be helpful to everyone. Thank you for your support!