This is the 22nd day of my participation in the August Wen Challenge.More challenges in August

preface

As we all know, string objects are almost everywhere in JavaScript, so the common methods of string objects need to be constantly reviewed and memorized. So today we’re going to talk about common methods of string objects in JavaScript.

The body of the

To be clear, all string methods do not modify the string itself (the string is immutable), and return a new string upon completion.

Characters of the method

CharAt () // get STR [0] at the specified position // HTML5, FromCharCode () // returns a String. FromCharCode (101, 102, 103) // converts ASCII codes to stringsCopy the code

String manipulation methods (common)

Find the specified string

IndexOf() retrieves the first occurrence of a character, or if not, -1 lastIndexOf() searches for the first occurrence starting from behind. If not, return -1Copy the code

Remove the blank

Trim () removes whitespace from both sides of the stringCopy the code

Case conversion

ToUpperCase () // All uppercase toLowerCase() // all lowercaseCopy the code

String concatenation and interception

String splicing

1. You can use concat as an array (to return a new string). A + sign is usually used to spell a stringCopy the code

String interception

There are many ways to intercept strings, but the more you remember, the more confusing it becomes, so just remember the easy ones

Substring () : start, end, include header, no tail, return a new string, unchanged substr() : start, end, include header, no tail, return a new string, unchanged substr() : Start from start, truncate length (recommended)Copy the code

String cutting

Split (): Splits a string into an array, leaving the original string unchanged (very common)

Var arr = str.split(",")Copy the code

String substitution

Replace (searchValue, replaceValue) // Parameter: searchValue: the value to be replaced replaceValue: the value to be replaced Note that a new string is returned, the original string is unchanged

var str = "abcd"
var newStr = str.replace("d","aaaa")
console.log(str)    // abcd
console.log(newStr)    // abcaaaa
Copy the code

example

Now that we have mastered these methods, we will review them thoroughly, so that we can remember firmly and remember well. Big Ice thought of a few sample questions, if you are interested, you can post the answers in the comments section

  • Intercept the string “Gold digging community, your fishing home!” “Touch fish”

  • “Juejinmoyumoyu” looks for all occurrences of “o” in the string

  • Replace all “o” in the string “juejinmoyumoyu” with “ai”

  • Determine the most frequently occurring character in the string “juejinmoyumoyu” and count the number of occurrences

Afterword.

Hello, I am the South Pole ice cube, a technology and appearance level proportional to the front-end engineer, advocating to solve front-end problems, I hope my blog has helped you.

Pay attention to me and walk together on the front road. Hey ~ 😛