Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

preface

  • In this article, we will look at the common methods of strings

Common methods for strings

  • A string is immutable after creation

  • Create a copy of the string before operating

  • increase

    • Concat concates one or more strings into a new string
    let str = 'hello'
    str.concat('vike') // hellovike
    Copy the code
  • delete

    • Slice Start position End position parameter
    let str = 'hello'
    str.slice(3) // 'lo'
    Copy the code
    • Substr Start position End position parameter
    let str = 'hello'
    str.substr(3) // 'lo'
    Copy the code
    • Substring Start position End position parameter
    let str = 'hello'
    str.substring(3) // 'lo'
    Copy the code
  • change

    • Trim trimLeft trimRight Before and after deletion The new character string is returned
    let str = ' hello world '
    str.trim() // 'hello world'
    Copy the code
    • Repeat takes an integer parameter indicating how many times to copy the string
    let str = 'hello'
    str.repeat(2) // 'hellohello'
    Copy the code
    • PadStart padEnd copies the string, and if it is shorter than the specified length, fills in the characters on the corresponding side until the length condition is met
    let str = 'hello'
    str.padStart(10) // ' hello'
    
    str.padStart(10.'. ') // '.....hello'
    Copy the code
    • ToLowerCase toUpperCase case case conversion
    let str = 'hello'
    str.toLowerCase() // 'hello'
    
    str.toUpperCase() // 'HELLO'
    Copy the code
  • check

    • ChatAt returns the character at the given index position
    let str = 'hello'
    str.chatAt(2) // 'l'
    Copy the code
    • IndexOf checks whether the passed argument is contained in the string. Returns an index value if not
    let str = 'hello'
    str.includes('e') / / 1
    Copy the code
    • Includes checks whether the passed parameters are included in the string. Returns true if so, false otherwise
    let str = 'hello'
    str.includes('o')  // true
    Copy the code
  • String splitting
    • Split returns an array of results by dividing the string as specified
    let str = 'hello world'
    str.split(' ') // ['hello','world']
    Copy the code
  • String regularization
    • matchAccepts a single argument, which can be a regular expression string or a singleRegExpObject that returns an array
    let text = "cat, bat, sat, fat";
    let pattern = /.at/;
    let matches = text.match(pattern);
    console.log(matches[0]); // "cat"
    Copy the code
    • searchAccepts a single argument, which can be a regular expression string or a singleRegExpObject, returns the matching index if found, -1 otherwise
    let text = "cat, bat, sat, fat"; let pos = text.search(/at/); console.log(pos); / / 1Copy the code
    • Replace takes two arguments the first as a match argument and the second as a replacement argument
    let text = 'hello'
    text.replace('he'.'eh') // 'ehllo'
    Copy the code

conclusion

  • Today’s lesson