Common methods for JavaScript strings

Common methods for JavaScript strings

Commonly used string methods include operation methods, conversion methods, and template matching methods.

1. Operation method

Commonly used string operation methods include adding, deleting, modifying, and searching.

Note: Once a string is created, it is immutable.

1.1 increase

Adding to a string is not to add content directly, but to create a copy of the string and then perform the operation.

  • + : concatenate strings
 let str = 'Hello'
 let str1 = 'world'
 console.log("+进行字符串拼接:",str+' '+str1)

operation result:
+Run results

  • ${}: concatenate strings
 // ${}
let str = 'Hello'
let str1 = 'world'
console.log(`拼接出来的字符串:${
      
      str} ${
      
      str1}`)

operation result:
${} running results

  • concat: used to concatenate one or more strings into a new string.
 // concat
 let str = 'Hello'
 let str1 = '  '
 let str2 = 'world'
 console.log("concat拼接出来的字符串:",str.concat(str1).concat(str2))

operation result:
concat running results

1.2 Delete

Deleting a string does not delete the contents of the original string, but creates a copy of the string for deletion.

-slice(start,end): The method can return the selected element from the existing string and return a new string, including the string from start to end (excluding the element).

         let str = 'Hello world'
        console.log("原字符串:",str)
        console.log("slice删除字符串:",str.slice(0,6))
        console.log(str.slice());//Hello world
        console.log(str.slice(0,-2))// Hello wor 倒过来删除了
        console.log(str.slice(0,100))//Hello world

operation result:
slice operation results

  • substr(): substr(string,start<,length>) extracts the string starting from the start position of string. length: the length of the string to be extracted. If length is the following situation, all characters of the entire string are returned.
    1. length is not specified
    2. length is empty
    3. length is a negative value
    4. length is greater than the length of the string
        let str = 'Hello world'
        console.log("原字符串:",str) //原字符串: Hello world
        console.log("substr删除字符串:",str.substr(0,6)) //substr删除字符串: Hello 
        console.log(str.substr()) //Hello world
        console.log(str.substr(0,-2)) // 为空
        console.log(str.substr(0,100)) //Hello world

operation result:
substr operation results

  • substring():substring(start,end) extracts the string starting from the start position of string and intercepting it to the end position (excluding end).
        let str = 'Hello world'
        console.log("原字符串:",str) //原字符串: Hello world
        console.log("substring删除字符串:",str.substring(0,6)) //substr删除字符串: Hello 
        console.log(str.substring()) //Hello world
        console.log(str.substring(0,-2)) // 为空
        console.log(str.substring(0,100)) //Hello world

operation result:
substring operation results

1.3 Change

  • trim(): Remove all spaces before, after, or before and after, and then return a new string. Only spaces at the beginning and end of a string can be removed, but spaces in the middle of the string cannot be removed. And it returns a new string, and a variable must be defined to receive the new string.
 let str = ' *  Hello JavaScript  *  '
 console.log("原字符串:",str) //原字符串:  *  Hello JavaScript  *  
 console.log("trim修改字符串:",str.trim()) // trim修改字符串: *  Hello JavaScript  *
       

operation result:
trim running results

  • repeat() receives an integer parameter, indicating how many times to copy the string, and then returns the result after splicing all copies.
        let str = 'Hello world'
        console.log("原字符串:",str) //原字符串: Hello world
        console.log('repeat()修改字符串:',str.repeat(2)) //repeat()修改字符串: Hello worldHello world

operation result:
repeat running results

  • padStart() is used for head completion and padEnd() is used for tail completion. Copies the string, and if it is less than the specified length, pads characters on the corresponding side until the length condition is met.
        let str = 'Hello world'
        console.log("原字符串:",str) //原字符串: Hello world
        console.log('padStart()修改字符串:',str.padStart(20,'*')) //padStart()修改字符串: *********Hello world
        console.log('padEnd()修改字符串:',str.padEnd(20,'*')) //padEnd()修改字符串: Hello world*********

operation result:
operation result

  • toLowerCase(), toUpperCase() case conversion.
let str = 'Hello world'
console.log("原字符串:",str) //原字符串: Hello world
console.log('toLowerCase()修改字符串:',str.toLowerCase()) // toLowerCase()修改字符串: hello world
console.log('toUpperCase()修改字符串:',str.toUpperCase()) // toUpperCase()修改字符串: HELLO WORLD
        

operation result:
operation result

2. Conversion method

split: Split the string into each item in the array according to the specified delimiter.

 let str = 'Hello world'
 console.log("split转换字符串:",str.split(' '))

operation result:
split running results

3. Template matching method

  • match(): receives a parameter, which can be a regular expression string or a RegExp object, and returns an array.
 let reg = /ab/
 let str = '12'
 let str1 = 'abash'
 console.log("match()匹配字符串:",str.match(reg))
 console.log("match()匹配字符串:",str1.match(reg))

operation result:
match running results
Note: match returns null if there is no matching result.

  • search() receives a parameter, which can be a regular expression string or a RegExp object. If found, it returns the matching index, otherwise it returns -1.
 let reg = /ab/
 let str = '12'
 let str1 = 'abash'
 console.log("search()匹配字符串:",str.search(reg))
 console.log("search()匹配字符串:",str1.search(reg))

operation result:
search running results

  • replace() receives two parameters, the first parameter is the matching content, and the second parameter is the replaced element (available function).
 let reg = 'ab'
 let str = 'abbchskkd'
 console.log("replace()匹配字符串:",str.replace(reg,'123'))

operation result:
replace running results

Guess you like

Origin blog.csdn.net/weixin_51938823/article/details/131755725