Some common methods of strings in JS

The following are some commonly used JavaScript string manipulation methods, classified in alphabetical order:

String operation methods

  • charAt (position): Returns the character at the specified position.
  • concat (string1, string2, …, stringN): Concatenates two or more strings.
  • includes (searchString, position): Determine whether a string contains another string.
  • endsWith (searchString, length): Determines whether a string ends with the specified string.
  • indexOf (searchValue, fromIndex): Returns the position where a specific character appears for the first time.
  • lastIndexOf (searchValue, fromIndex): Returns the position of the last occurrence of a specific character.
  • localeCompare (compareString, locales, options): Compares two strings and returns a number representing the comparison result.
  • match (regexp): Finds a match for one or more regular expressions.
  • repeat (count): Repeat the string a specified number of times.
  • replace (searchValue, replaceValue): Replace specified characters appearing in a string.
  • search (regexp): Retrieve a specified substring or regular expression in a string.
  • slice (startIndex, endIndex): Extract a part of the string and return a new string.
  • split (separator, limit): Split a string into an array of substrings.
  • startsWith (searchString, length): Determine whether a string starts with the specified string.
  • substr (startIndex, length): Extract the substring of the specified length at the specified position in the string.
  • substring (startIndex, endIndex): Extract characters between two specified positions in the string.
  • toLocaleLowerCase (): Converts all characters in the string to lowercase letters.
  • toLocaleUpperCase (): Converts all characters in the string to uppercase letters.
  • toLowerCase (): Converts all characters in the string to lowercase letters.
  • toString (): Returns a string.
  • toUpperCase (): Converts all characters in the string to uppercase letters.
  • trim (): removes whitespace characters at both ends of the string and returns a new string.
  • valueOf (): Returns the original value of a string object.

Regular expression operation method

  • exec (string): Search for a match in a string and return an array containing matching information.
  • test (string): Checks whether a string matches a pattern, returns true or false.
  • compile (): Compile the regular expression into a reusable object.

Instructions:

// 字符串操作方法示例
const str = "Hello World";
console.log(str.charAt(0)); // H
console.log(str.concat("!!!")); // Hello World!!!
console.log(str.includes("World")); // true
console.log(str.endsWith("rld")); // true
console.log(str.indexOf("World")); // 6
console.log(str.lastIndexOf("l")); // 9
console.log(str.localeCompare("hello world")); // 1
console.log(str.match(/o/gi)); // [ 'o', 'o' ]
console.log(str.repeat(3)); // Hello WorldHello WorldHello World
console.log(str.replace("World", "JavaScript")); // Hello JavaScript
console.log(str.search(/o/gi)); // 4
console.log(str.slice(1, 4)); // ell
console.log(str.split(" ")); // [ 'Hello', 'World' ]
console.log(str.startsWith("Hello")); // true
console.log(str.substr(1, 3)); // ell
console.log(str.substring(1, 4)); // ell
console.log(str.toLocaleLowerCase()); // hello world
console.log(str.toLocaleUpperCase()); // HELLO WORLD
console.log(str.toLowerCase()); // hello world
console.log(str.toString()); // Hello World
console.log(str.toUpperCase()); // HELLO WORLD
console.log(str.trim()); // Hello World
console.log(str.valueOf()); // Hello World

// 正则表达式操作方法示例
const regex = /\w+/;
console.log(regex.exec(str)); // [ 'Hello', index: 0, input: 'Hello World', groups: undefined ]
console.log(regex.test(str)); // true
console.log(regex.compile(/\d+/)); // /\d+/

Guess you like

Origin blog.csdn.net/Jet_Lover/article/details/130838230