This is the 11th day of my participation in the August More Text Challenge

Common methods for JavaScript built-in String objects

Creating a string

  • var str = "hello world";

Get the length of the string

  • str.length

charAt()

  • Purpose: Get the character of the specified index of the string
  • Grammar:str.charAt(index)
  • indexValues for0str.length - 1Negative numbers are not supported
  • Reference: String. The prototype. The charAt ()
  • Example:
var str = 'hello'
console.log(str.charAt(0))  // h
Copy the code
  • Note: If not writtenindex, the defaultindex0
  • And index operation[]In contrast, index access does not returnundefinedAnd thecharAtReturns an empty string. In other words,charAtAlways return a string.

indexOf()

  • Purpose: To get the index of the first occurrence of a given string in the current string, or return if not found- 1
  • Grammar:str.indexOf(searchValue [, fromIndex])
  • searchValueIs the string to be found.
  • Reference: String. Prototype. IndexOf ()
  • Example:
console.log('hello world'.indexOf('w'))  / / 6

'hello world'.indexOf(' ')  / / 0
Copy the code

lastIndexOf()

  • Purpose: To find the index of the first occurrence of a given string in the current string from back to front, if not found, return- 1
  • Grammar:str.lastIndexOf(searchValue[, fromIndex])
  • Reference: String. The prototype. The lastIndexOf ()
  • Example:
var str = 'Brave new world'
console.log(str.lastIndexOf('new'))  / / 6
Copy the code

includes()

  • Purpose: Determine whether the current string contains the given string
  • Grammar:str.includes(searchString[, position])
  • searchStringIs the string to be found
  • positionFor fromstrAt which index of, the search for substrings begins. The default is0.
  • Reference: String. Prototype. Includes ()
  • Example:
// includes is case sensitive
'Blue Whale'.includes('blue'); // returns false
Copy the code

startsWith()

  • Purpose: Determine whether the current string has been given the beginning of the string
  • Grammar:str.startsWith(searchString[, position])
  • positionIs the initial search location
  • The reference String. Prototype. StartsWith ()
  • Example:
// startsWith is case-sensitive
var str = 'hello'
console.log(str.startsWith('Hell'))  // false
Copy the code

endsWith()

  • Purpose: Determine whether the current string ends with the given string
  • Grammar:str.endsWith(searchString[, length])
  • lengthstrThe default value isstr.length
  • Reference: String. Prototype. EndsWith ()
  • Example:
var str = 'hello'
console.log(str.endsWith('llo'))  // true

// length specifies the length of the string, which is equivalent to the index without the string
var str = "To be, or not to be, that is the question.";

alert( str.endsWith("question."));// true
alert( str.endsWith("to be"));// false
alert( str.endsWith("to be".19));// true

Copy the code

padStart()

  • Purpose: Padding the current string with the given string, starting from the left, so that the final length reaches the specified length, the excess will be truncated.
  • Grammar:str.padStart(targetLength [, padString])
  • padStringDefault is space' '
  • Reference: String. Prototype. PadStart ()
  • Example:
console.log('abc'.padStart(10.'nihao'))  // nihaoniabc
Copy the code

padEnd()

  • Purpose: Padding the current string with the given string, starting from the right, so that the final length reaches the specified length, the excess will be truncated.
  • Grammar: ` `
  • Reference: String. Prototype. PadEnd ()
  • Example:
console.log('abc'.padEnd(8.The '*'))  // abc*****
Copy the code

repeat()

  • Purpose: Repeat the current string a given number of times to return a new string.
  • Grammar:str.repeat(count)
  • If I don’t give youcount.countThe default is0
  • ifcount0, returns an empty string
  • Reference: String. Prototype. Repeat ()
  • Example:
'abc'.repeat()  / /"
'abc'.repeat(0)  / /"
'abc'.repeat(2)  // 'abcabc'
// 3.7 is automatically truncated to 3
'abc'.repeat(3.7)  // 'abcabcabc'
Copy the code

concat()

  • Purpose: Concatenate one or more strings and return a new string
  • Grammar:str.concat(str2, [, ...strN])
  • Performance:Highly recommended use++ =Instead ofconcatmethods.
  • Reference: String. The prototype. The concat ()
  • Example:
' '.concat(1.2.3)  / / '123'
'hello'.concat(' '.'world')  // 'hello world'
Copy the code

replace()

  • Purpose: To replace the old string in the current string with a new string, replacing only the first match and returning a brand new string
  • Grammar:str.replace(regexp|substr, newSubStr|function)
  • Reference: String. The prototype. The replace ()
  • Example:
var str = 'apple'
var newStr = str.replace('apple'.'pear')
console.log(newStr)  // pear
Copy the code

search()

  • Purpose: To find the index of the first occurrence of a given string in the current string
  • Grammar:str.search(regexp)
  • regexpIs a regular expression object that is used if a non-regular expression is passed innew RegExp(regexp)
  • Reference: String. The prototype. The search ()
  • Example:
console.log('hello'.search('el'))  / / 1
Copy the code

split()

  • Purpose: Split the current string into arrays based on the separator and the number of cuts and return them
  • Grammar:str.split([separator[, limit]])
  • If no argument is passed, an array with the original string is returned. ifseperator' ', returns an array of single characters. This method is closely related toPythonstr.split()Not quite the same,PythonDefaults to whitespace characters.
  • limitRepresents the maximum length of the returned array (PythonOf the string insplit()Method, the second parametermaxsplitRepresents the maximum number of cuts)
  • Reference: String. The prototype. The split ()
  • Example:
var str = 'hello world'
var arr = str.split('o')
console.log(arr)  // ['hell', 'w', 'rld']
Copy the code

substr()substring()

  • Purpose: To intercept a substring from the current string
  • Note:substr()Is a legacy function, use it whenever possiblesubstring()Instead of it
  • substr()Grammar:str.substr(start[, length])It’s kind of likeCThe language of thesubstr(pos, len)Function.
  • substring()Grammar:str.substring(indexStart[, indexEnd])Not at the time of interceptionindexEndThe character corresponding to the index
  • Reference: String. The prototype. The substr ()
  • Reference: String. The prototype. The substring ()
  • Example:
'hello'.substring(1.3)  // 'el'
Copy the code

toLowerCase()

  • Purpose: To convert the current string to lowercase and return a new string
  • Grammar:str.toLowerCase()
  • Reference: String. Prototype. ToLowerCase ()
  • Example:
'Abc'.toLowerCase()  // abc
Copy the code

toUpperCase()

  • Purpose: To convert the current string to uppercase and return a new string
  • Grammar:
  • Reference: String. The prototype. The toUpperCase ()
  • Example:
'Abc'.toUpperCase()  // ABC
Copy the code

trim()

  • Purpose: Remove whitespace characters from both sides of the current string and return a new string
  • Grammar:str.trim()
  • Reference: String. The prototype. The trim ()
  • Example:
'\t\nfoo \n'.trim()
'foo'
Copy the code