Summary of common string methods in JavaScript

Table of contents

length gets the length of the string

Get the position of a character in a string

indexOf() Gets the position of occurrence (search from front to back)

lastIndexOf() gets the position of occurrence (search from back to front)

search() regular expression search

includes() finds a string in a string

startsWith() determines whether it starts with a string

endsWith() determines whether to end with a field

charAt() finds the character at the specified subscript 

case conversion

toUpperCase() to uppercase

toLowerCase() converts to lowercase

Remove string whitespace characters

trim() removes whitespace characters on both sides

trimEnd() removes trailing blank characters

trimStart() removes the first blank character

intercept string

substring() interception

substr() is deprecated

split() split method

connection string

concat() concatenates strings

replace string method

replace() replacement method

repeat string

repeat() repeat method


Foreword:

All string methods return new strings without changing the original string (this is much easier to distinguish than arrays~)

length gets the length of the string

var str = 'bad girl';
console.log(str.length); // 8

Get the position of a character in a string

indexOf() Gets the position of occurrence (search from front to back)

str.indexOf(searchString, position) // 第一个参数是要查找的字符,第二个参数是开始查找的位置
// 第二个参数可省略,默认从第一个字符开始查找

This method receives two parameters. The first parameter is the character to be searched, and the second parameter is the starting position of the search. If the second parameter is not passed, the search starts from 0 by default; if the parameter is less than 0, the default is also from 0 to start searching; search is case-sensitive~

This method returns the index position of the search field, if not found, returns -1

 var str = 'good good study, day day up';
 console.log(str.indexOf('a')); // 18 取得是第一个day中的a

 var str = 'good good study, day day up';
 console.log(str.indexOf('a', 19)); // 22 从第19个字符开始查找

 var str = 'good good study, day day up';
 console.log(str.indexOf('e')); // -1 没找到字符e

lastIndexOf() gets the position of occurrence (search from back to front)

The parameters are the same as the indexOf() method in usage. If the second parameter is greater than or equal to the length of the array, it means that the entire array will be searched; if it is a negative value, it is considered to start searching from 0;

var str = 'good good study, day day up';
console.log(str.lastIndexOf('a')); // 22 取得是第二个day中的a
console.log(str.lastIndexOf('a', -22)); // -1 负数默认从索引位置0处取,0位置是g,所以结果为-1

search() regular expression search

This method executes the regular expression to search and match the String object;

str.search(regexp)

The passed parameter is a regular expression object. If a non-regular expression object is passed in, it will be implicitly converted to a regular expression object using new RegExp(regexp);

If the match is successful, return the index value of the first match of the regular expression in the string, otherwise -1;

var str = 'good good study, day day up';
console.log(str.search(/oo/)); // 1
console.log(str.search(/^oo$/)); // -1
console.log(str.search('o')); // 1

includes() finds a string in a string

This method performs a case-sensitive search to determine whether a string can be found in another string, and returns true if it can be found, otherwise returns false; it also accepts two parameters, the first parameter is the one to find string, the second string is the starting search position;

str.includes(searchString)
str.includes(searchString, position)
var str = 'good good study, day day up';
console.log(str.includes('oo')); // true
console.log(str.includes('oo', 8)); // false

startsWith() determines whether it starts with a string

The startsWith() method is used to determine whether the current string starts with another substring (case-sensitive), if it returns true, otherwise it returns false;

str.startsWith(searchString)
str.startsWith(searchString, position)

There are also two parameters, the first parameter represents the substring, and the second parameter is the position to start searching;

var str = 'good good study, day day up';
console.log(str.startsWith('good')); // true
console.log(str.startsWith('study')); // false

endsWith() determines whether to end with a field

The endsWith() method is used to determine whether the current string ends with another substring (case-sensitive), if it returns true, otherwise it returns false;

str.startsWith(searchString)
str.startsWith(searchString, length)

There are also two parameters, the first parameter indicates the substring, the second parameter indicates the length of the intercepted str, if not passed, the default is str.length 

var str = 'good good study, day day up';
console.log(str.endsWith('up')); // true
console.log(str.endsWith('study')); // false
console.log(str.endsWith('study',15)); // true

charAt() finds the character at the specified subscript 

This method returns the character of the specified subscript; the value of the parameter index is between 0 and str-length-1, if it is not in this range, this method will return an empty string.

str.charAt(index)
var str = 'good good study, day day up';
console.log(str.charAt(3)); // d

case conversion

toUpperCase() to uppercase

str.toUpperCase()
var str = 'good good study, day day up';
console.log(str.toUpperCase()); // GOOD GOOD STUDY, DAY DAY UP

toLowerCase() converts to lowercase

str.toLowerCase()
var str1 = 'GOOD GOOD STUDY, DAY DAY UP';
console.log(str.toLowerCase()); // good good study, day day up

Remove string whitespace characters

trim() removes whitespace characters on both sides

This method deletes blank characters on both sides of the beginning and end, including spaces, tabs, and non-newline spaces, and all line terminators (such as LF/CR, etc.)

str.trim()
 var str = '  good good study, day day up  ';
 console.log(str); //   good good study, day day up  
 console.log(str.trim()); // good good study, day day up

trimEnd() removes trailing blank characters

This method removes whitespace characters at the end of the string. This method is equivalent to trimRight();

var str = 'good good study, day day up  ';
console.log(str.length); // 29  
console.log(str.trimEnd().length); // 27

trimStart() removes the first blank character

This method removes whitespace characters at the beginning of the string. This method is equivalent to trimLeft();

  var str = '   good good study, day day up';
  console.log(str.length); // 30  
  console.log(str.trimStart().length); // 27

intercept string

slice() interception

The slice() method intercepts a part of a string and returns a new string;

str.slice(beginIndex)
str.slice(beginIndex, endIndex)

Receive two parameters, the first parameter is the index position to start intercepting , the second parameter is the index position to stop intercepting (optional, if not passed, the default is to the end of the string); left close and right open

If the parameter is a negative value, it is calculated by beginIndex+strLength or endIndex+strLength; strLength is the length of the string

var str = 'good good study, day day up';
console.log(str.slice(1,6)); // ood g
console.log(str.slice(1,-9)); // 从1到(27-9)取字符串,结果为good good study, d

substring() interception

This method returns a subset of a string between the start index and the end index;

str.substring(indexStart)
str.substring(indexStart, indexEnd)

There are two parameters, the first parameter indicates the start index of the interception, the second parameter is optional, and indicates the cut-off position of the interception ( left closed and right open ), a few points for attention:

  • Returns an empty string if the two arguments are the same
  • If the parameter is less than 0 or NaN, it is treated as 0;
  • If the first argument is greater than the second argument, the result is the same as in order
var str = 'good good study, day day up';
console.log(str.substring(1,6)); // ood g
console.log(str.substring(1,-9)); // 相当于(0,1) g
console.log(str.substring(8, 1)); // 相当于(1,9) ood goo

The difference from the slice method is that the processing method for negative parameters is different~

substr() is deprecated

split() split method

This method is used to separate the string into substrings at the specified delimiter character and form them into an array of substrings ;

str.split()
str.split(separator)
str.split(separator, limit)

The first parameter represents a separator or a regular expression , such as "|", it means to split according to the "|" in the string. After finding the separator, delete it from the string. If the parameter is a regular expression, And if you add parentheses, you don’t need to delete them, and return the entire array

The second parameter indicates the number of segments to limit the return. If it is divided into 5 pieces, if this value is set to 3, only the array composed of the first three pieces will be returned

var str = 'good good study, day day up';
console.log(str.split(' ')); // ['good', 'good', 'study,', 'day', 'day', 'up']
console.log(str.split(' ', 3));  // ['good', 'good', 'study,']
console.log(str.split(/oo/)); // ['g', 'd g', 'd study, day day up']
console.log(str.split(/oo/, 2));  //  ['g', 'd g']
console.log(str.split(/(oo)/)); // ['g', 'oo', 'd g', 'oo', 'd study, day day up']
console.log(str.split(/(oo)/, 2));  //  ['go', 'oo']

The join() method of the array has the opposite effect

connection string

concat() concatenates strings

Can concatenate multiple strings and return the newly concatenated string

concat(str1, str2, /* …, */ strN)
var str = 'good good study, day day up';
var str1 = 'and earn money';
var str2 = 'make world beauty';
console.log(str.concat(str1,str2)); // good good study, day day upand earn moneymake world beauty

replace string method

replace() replacement method

This method has been mentioned in the previous regular expression, it returns the new string replaced by the replacement value

str.replace(regexp|substr, newSubStr|function)

The first parameter can be a regular expression or a string, and the second parameter can be a new string or function to be replaced (a function used to create a new substring, and the return value of the function is used as a new string replace); only the first matched string can be replaced; if you use regular expressions to match and replace, you can use the /g flag

var str = 'good good study, day day up';
console.log(str.replace('good', 'bad')); // bad good study, day day up
console.log(str.replace(/good/, 'bad')); // bad good study, day day up
console.log(str.replace(/good/g, 'bad')); // bad bad study, day day up

repeat string

repeat() repeat method

str.repeat(count)

Repeat str count times

var str = 'good';
console.log(str.repeat(3)); // goodgoodgood

Guess you like

Origin blog.csdn.net/ks795820/article/details/127977746