17 common string methods and usage

1 chartAt () method

<script type='text/javascript'>
    var str = "hello word"
    console.log(str.charAt(3))   // l
</script>
Copy the code

ChartAt (x) returns the character at position X in the string, with subscripts starting at 0

2 charCodeAt () method

<script type='text/javascript'>
    var str = "hello word"
    console.log(str.charCodeAt(3))   //  108
</script>
</script>
Copy the code

CharCodeAt (x) returns the Unicode value of the character at position X in the string

3 Concat () method

<script type='text/javascript'>
    var str = "hello word!"
    console.log(str.concat("Hello"."china")) 
      / / hello word! Hello China
</script>
Copy the code

The concat() method is used to concatenate one or more strings, leaving the original string unchanged and returning a new concatenated string

4 indexOf (string to find, start position) method

<script type='text/javascript'>
    var str = "hello word!"
    console.log(str.indexOf("w")) / / 6
</script>
Copy the code

The indexOf () method searches for a value in the string and returns the subscript (index) of that value if it is found, or -1 if it is not. Start is an optional argument that specifies the position at which the string starts searching. The default value is 0

5 lastIndexOf (string to find, start position) method

<script type='text/javascript'>
    var str = "hello wowrd!"
    console.log(str.lastIndexOf("w")) / / 8
</script>
Copy the code

The lastIndexOf method returns the index of the last occurrence of the specified text in the string, or -1 if it is not found. Start is an optional argument that specifies where the string search starts. The default value is String.length-1

6 startsWith () method

<script type='text/javascript'>
    let str = "qwertyui";
    console.log(str.startsWith("w"))//false
</script>
Copy the code

Checks if the string begins with… startsWith() returns Boolean

7 endsWith () method

<script type='text/javascript'>
    let str = "qwertyui";
    console.log(str.endsWith("yui"))//true
</script>
Copy the code

EndsWith () returns a Boolean

Repeat 8 () method

<script type='text/javascript'>
    let str = "Cutest";
    console.log(str.repeat(5))
    // The loveliest, the loveliest, the loveliest, the loveliest
</script>
Copy the code

Repeat (number of times to copy)

9 padStart () method

<script type='text/javascript'>
    console.log("h".padStart(3."wj"))
    //wjh
</script>
Copy the code

The padStart() method prefills the string (3 is the length of the fill, wj is the content of the fill)

10 padEnd () method

<script type='text/javascript'>
     console.log("Call".padEnd(5.Wang Ai))
     // Just call Wang Cuai
</script>
Copy the code

The padEnd() method fills in the string (5 is the length after filling)

11 the replace () method

<script type='text/javascript'>
    var str = "html javascript vue"
    var str1 = str.replace(/javascript/i."css")
    console.log(str1)
  //html css vue
</script>
Copy the code

The replace() method is used to replace some characters in a string with other characters, or to replace a substring that matches a regular expression

12 slice (start, end) method

<script type='text/javascript'>

    var str = "html javascript vue"
    
    console.log(str.slice(0.3))//htm
 
</script>
Copy the code

The slice method extracts a portion of a string and returns a new string starting (including start) through end, but excluding all characters up to end

13 string. The split () method

<script type='text/javascript'>
    var str = "How are you doing today?";
    var n = str.split("");
    console.log(n)
    // ["How", "are", "you", "doing", "today?"]

</script>
Copy the code

The split() method splits a string into an array of strings. If the empty string (“”) is used as separator, each character in stringObject is separated. The split() method does not change the original string.

14 Substring (from, to) Method Method

<script type='text/javascript'>
    var str = "Hello world!";
    document.write(str.substring(3) + "<br>");//lo world!
    document.write(str.substring(3.7));// lo w
</script>
Copy the code

The substring() method is used to extract the character of a string intermediate between two specified subscripts. The substring() method returns a substring that includes the beginning character, but not the end character.

15 toUpperCase () method

<script type='text/javascript'>
    var str = "Hello world!";
    document.write(str.toUpperCase());//HELLO WORLD!
</script>
Copy the code

The toUpperCase() method converts English strings toUpperCase

16 toLowerCase () method

<script type='text/javascript'>

    var str = "Hello world!";
    
    document.write(str.toLowerCase());//hello world!

</script>
Copy the code

The toLowerCase() method converts the English string toLowerCase

17 includes () method

<script type='text/javascript'>
    var str = "Hello world!";
    let b = str.includes('Hello');
    console.log(b)//true
</script>
Copy the code

The includes() method checks to see if the array includes a value that results in true or false

The above is a personal summary of some of the string methods, if there are other common methods, welcome to leave a comment, learn to exchange!!