Detailed explanation of common methods of JavaScript String

Detailed explanation of common methods of JavaScript String

1. charAt

Returns the specified character from a string

grammar

str.charAt(index)

parameter

index

An integer between 0 and the length of the string minus 1. (0~length-1)

If no index is provided, charAt() will use 0.

Return Value

Returns the specified character, or an empty string if the specified index value is out of range.

insert image description here

2. concat

Concatenates one or more strings with the original string to form a new string and returns it. The concat method does not affect the original string.

grammar

str.concat(str2, [, ...strN])

parameter

str2 [, …strN]

The string to be concatenated to str.

Return Value

A new string containing the connection string provided as a parameter.

insert image description here

3. indexOf

Returns the index of the first occurrence of the specified character in the String object, searching from fromIndex. If the value is not found, -1 is returned.

grammar

str.indexOf(searchValue [, fromIndex])

parameter

searchValue

The string value to be searched.
If no exact string is provided, searchValue is coerced to "undefined" and the value is then searched for within the current string.

fromIndex (optional)

The number indicates where to start searching. Can be any integer, default value is 0.
If the value of fromIndex is less than 0, or greater than str.length, then the search starts at 0 and str.length respectively. (Translator's note: if the value of fromIndex is less than 0, it is equivalent to being empty; if the value of fromIndex is greater than or equal to str.length, the result will be returned directly as -1.)

Return Value

Returns the index of the first occurrence of the search string searchValue, or -1 if not found.

insert image description here

Special circumstances

1. If the search string searchValue is an empty string and the fromIndex value is empty, or the fromIndex value is less than the length of the search string, the return value is the same as the fromIndex value below

2. If the search string searchValue is an empty string, and the fromIndex value is greater than or equal to the length of the string, the length of the string will be returned directly

4. lastIndexOf

The opposite of indexOf, it just searches from the back to the front.

5.match

Search returns a string that matches the regular expression.

grammar

str.match(regexp)

parameter

regexp

A regular expression object. If a non-regular expression object is passed in, it will be implicitly converted to a RegExp using new RegExp(obj). If you don't give any arguments and use the match() method directly, you will get an Array containing an empty string: [""] .

Return Value

  • If the g flag is used, all results matching the full regular expression will be returned, but capturing groups will not be returned.
  • If the g flag is not used, only the first complete match and its associated capturing groups (Array) are returned. In this case, the returned item will have additional properties as described below.

insert image description here

6. replace

Returns a new string with some or all matches of pattern replaced by replacement. The pattern can be a string or a regular expression, and the replacement can be a string or a callback function to be called on each match. If pattern is a string, only the first occurrence is replaced.

The original string will not be changed.

grammar

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

parameter

regexp (pattern)

A RegExp object or its literal. The content matched by this regular expression will be replaced by the return value of the second parameter.

substr (pattern)

A string that will be replaced by newSubStr. It is treated as a whole string, not a regular expression. Only the first occurrence is replaced.

newSubStr (replacement)

The string used to replace the matching part of the first parameter in the original string. Some special variable names can be interpolated in this string. See Using Strings as Parameters below.

function (replacement)

A function that creates a new substring whose return value replaces any matches of the first argument. See Specifying a function as an argument below.

Return Value

A new string with some or all of the matches in the replacement pattern replaced.

insert image description here

7. toLowerCase

Converts a string value to lowercase and returns

grammar

str.toLowerCase()

Return Value

A new string representing the calling string converted to lower case.

insert image description here

8. toUpperCase

The opposite of toLowerCase, convert to uppercase

9. Substring

Returns a subset of a string between the start index and the end index, or from the start index until the end of the string.

grammar

str.substring(indexStart[, indexEnd])

parameter

indexStart

The index of the first character to be intercepted. The character at this index position is used as the first letter of the returned string.

indexEnd

Optional. An integer between 0 and the length of the string. The characters indexed by this number are not included in the truncated string.

Return Value

A new string containing the specified portion of a given string.

insert image description here

Summarize

This article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM!

1. charAt

Returns the specified character from a string

grammar

str.charAt(index)

parameter

index

An integer between 0 and the length of the string minus 1. (0~length-1)

If no index is provided, charAt() will use 0.

Return Value

Returns the specified character, or an empty string if the specified index value is out of range.

insert image description here

2. concat

Concatenates one or more strings with the original string to form a new string and returns it. The concat method does not affect the original string.

grammar

str.concat(str2, [, ...strN])

parameter

str2 [, …strN]

The string to be concatenated to str.

Return Value

A new string containing the connection string provided as a parameter.

insert image description here

3. indexOf

Returns the index of the first occurrence of the specified character in the String object, searching from fromIndex. If the value is not found, -1 is returned.

grammar

str.indexOf(searchValue [, fromIndex])

parameter

searchValue

The string value to be searched.
If no exact string is provided, searchValue is coerced to "undefined" and the value is then searched for within the current string.

fromIndex (optional)

The number indicates where to start searching. Can be any integer, default value is 0.
If the value of fromIndex is less than 0, or greater than str.length, then the search starts at 0 and str.length respectively. (Translator's note: if the value of fromIndex is less than 0, it is equivalent to being empty; if the value of fromIndex is greater than or equal to str.length, the result will be returned directly as -1.)

Return Value

Returns the index of the first occurrence of the search string searchValue, or -1 if not found.

insert image description here

Special circumstances

1. If the search string searchValue is an empty string and the fromIndex value is empty, or the fromIndex value is less than the length of the search string, the return value is the same as the fromIndex value below

2. If the search string searchValue is an empty string, and the fromIndex value is greater than or equal to the length of the string, the length of the string will be returned directly

4. lastIndexOf

The opposite of indexOf, it just searches from the back to the front.

5.match

Search returns a string that matches the regular expression.

grammar

str.match(regexp)

parameter

regexp

A regular expression object. If a non-regular expression object is passed in, it will be implicitly converted to a RegExp using new RegExp(obj). If you don't give any arguments and use the match() method directly, you will get an Array containing an empty string: [""] .

Return Value

  • If the g flag is used, all results matching the full regular expression will be returned, but capturing groups will not be returned.
  • If the g flag is not used, only the first complete match and its associated capturing groups (Array) are returned. In this case, the returned item will have additional properties as described below.

insert image description here

6. replace

Returns a new string with some or all matches of pattern replaced by replacement. The pattern can be a string or a regular expression, and the replacement can be a string or a callback function to be called on each match. If pattern is a string, only the first occurrence is replaced.

The original string will not be changed.

grammar

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

parameter

regexp (pattern)

A RegExp object or its literal. The content matched by this regular expression will be replaced by the return value of the second parameter.

substr (pattern)

A string that will be replaced by newSubStr. It is treated as a whole string, not a regular expression. Only the first occurrence is replaced.

newSubStr (replacement)

The string used to replace the matching part of the first parameter in the original string. Some special variable names can be interpolated in this string. See Using Strings as Parameters below.

function (replacement)

A function that creates a new substring whose return value replaces any matches of the first argument. See Specifying a function as an argument below.

Return Value

A new string with some or all of the matches in the replacement pattern replaced.

insert image description here

7. toLowerCase

Converts a string value to lowercase and returns

grammar

str.toLowerCase()

Return Value

A new string representing the calling string converted to lower case.

insert image description here

8. toUpperCase

The opposite of toLowerCase, convert to uppercase

9. Substring

Returns a subset of a string between the start index and the end index, or from the start index until the end of the string.

grammar

str.substring(indexStart[, indexEnd])

parameter

indexStart

The index of the first character to be intercepted. The character at this index position is used as the first letter of the returned string.

indexEnd

Optional. An integer between 0 and the length of the string. The characters indexed by this number are not included in the truncated string.

Return Value

A new string containing the specified portion of a given string.

insert image description here

Summarize

This article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • JS method to delete a character in String
  • Difference between optString and getString methods in JSON
  • jsp method to determine whether a list contains a string
  • Detailed explanation of common methods of JavaScript String object
  • Summary of Common Methods of JavaScript String Object
  • Detailed explanation of the use of toString() method in JavaScript

<<:  The process of quickly converting mysql left join to inner join

>>:  Several methods of horizontal and vertical centering of div content using css3 flex

Recommend

MySQL Innodb key features insert buffer

Table of contents What is insert buffer? What are...

Vue uses vue meta info to set the title and meta information of each page

title: vue uses vue-meta-info to set the title an...

Analysis of GTK treeview principle and usage

The GtkTreeView component is an advanced componen...

How to install pip package in Linux

1. Download the pip installation package accordin...

Use of Linux crontab command

1. Command Introduction The contab (cron table) c...

SQL interview question: Find the sum of time differences (ignore duplicates)

When I was interviewing for a BI position at a ce...

How to build an ELK log system based on Docker

Background requirements: As the business grows la...

How to call the browser sharing function in Vue

Preface Vue (pronounced /vjuː/, similar to view) ...

How to achieve seamless token refresh

Table of contents 1. Demand Method 1 Method 2 Met...

Detailed explanation of Linux curl form login or submission and cookie usage

Preface This article mainly explains how to imple...

How to deploy python crawler scripts on Linux and set up scheduled tasks

Last year, due to project needs, I wrote a crawle...

MySQL uses covering index to avoid table return and optimize query

Preface Before talking about covering index, we m...

jQuery implements the function of adding and deleting employee information

This article shares the specific code of jQuery t...

mysql splits a row of data into multiple rows based on commas

Table of contents Separation effect Command line ...

Sharing of two website page translation plug-ins

TranslateThis URL: http://translateth.is Google T...