Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common methods of String in JavaScript

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces what are the common methods of String in JavaScript. It is very detailed and has certain reference value. Friends who are interested must finish it!

1 、 charAt

Returns the specified character from a string

Syntax str.charAt (index) parameter index

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

If no index is provided, charAt () uses 0.

Return value

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

2 、 concat

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

Syntax str.concat (str2, [,... strN]) parameter str2 [,... StrN]

A string to connect to str is required.

Return value

A new string containing the connection string provided by the parameter.

3 、 indexOf

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

Syntax str.indexOf (searchValue [, fromIndex]) parameter searchValue

The string value to be found.

If no exact supply string is provided, searchValue is forced to be set to "undefined" and then looks for this value in the current string.

FromIndex (optional)

The number indicates where to start the search. It can be any integer, and the default value is 0.

If the value of fromIndex is less than 0 or greater than str.length, the search starts at 0 and str.length, respectively. (translator's note: the value of fromIndex is less than 0, which is equivalent to null; the value of fromIndex is greater than or equal to str.length, then the result will directly return-1. )

Return value

The index of the first occurrence of the looked up string searchValue, if not found, returns-1.

Special circumstances

1. If the searched string searchValue is an empty string and the fromIndex value is empty, or if the fromIndex value is less than the length of the looked up string, the return value is the same as the following fromIndex value

two。 If the searched 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

In contrast to indexOf, it's just looking from back to front.

5 、 match

The retrieval returns the result of a string matching regular expression.

Syntax str.match (regexp) parameter regexp

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

Return value

If you use the g flag, all results that match the full regular expression are returned, but no capture group is returned.

If the g flag is not used, only the first full match and its associated capture group (Array) are returned. In this case, the returned project will have other properties as described below.

6 、 replace

Returns a new string that replaces some or all of the pattern matches with the replacement value (replacement). The pattern can be a string or a regular expression, and the replacement value can be a string or a callback function that is called every match. If pattern is a string, only the first match is replaced.

The original string does not change.

Grammar

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

Parameter regexp (pattern)

A RegExp object or its literal quantity. The content matched by the rule is replaced by the return value of the second parameter.

Substr (pattern)

A string to be replaced by newSubStr. It is treated as an entire string rather than a regular expression. Only the first match 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. Refer to the following using a string as a parameter.

Function (replacement)

A function used to create a new substring whose return value replaces the result matched by the first parameter. Refer to the following to specify a function as an argument.

Return value

One or all matches a new string that is replaced by an alternative pattern.

7 、 toLowerCase

Converts the string value to lowercase and returns

Syntax str.toLowerCase () returns a value

A new string that represents the call string converted to lowercase.

8 、 toUpperCase

Contrary to toLowerCase, turn to uppercase

9 、 substring

Returns a subset of a string from the start index to the end index, or from the start index to the end of the string.

Syntax str.substring (indexStart [, indexEnd]) parameter indexStart

The index of the first character to be intercepted, and the character of the index position as the first letter of the returned string.

IndexEnd

Optional. An integer between 0 and the length of a string in which characters indexed are not included in the intercepted string.

Return value

A new string that contains the specified part of the given string.

Summary

This is the end of this article, hope to help you, but also hope you can pay more attention to more content!

1 、 charAt

Returns the specified character from a string

Syntax str.charAt (index) parameter index

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

If no index is provided, charAt () uses 0.

Return value

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

2 、 concat

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

Syntax str.concat (str2, [,... strN]) parameter str2 [,... StrN]

A string to connect to str is required.

Return value

A new string containing the connection string provided by the parameter.

3 、 indexOf

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

Syntax str.indexOf (searchValue [, fromIndex]) parameter searchValue

The string value to be found.

If no exact supply string is provided, searchValue is forced to be set to "undefined" and then looks for this value in the current string.

FromIndex (optional)

The number indicates where to start the search. It can be any integer, and the default value is 0.

If the value of fromIndex is less than 0 or greater than str.length, the search starts at 0 and str.length, respectively. (translator's note: the value of fromIndex is less than 0, which is equivalent to null; the value of fromIndex is greater than or equal to str.length, then the result will directly return-1. )

Return value

The index of the first occurrence of the looked up string searchValue, if not found, returns-1.

Special circumstances

1. If the searched string searchValue is an empty string and the fromIndex value is empty, or if the fromIndex value is less than the length of the looked up string, the return value is the same as the following fromIndex value

two。 If the searched 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

In contrast to indexOf, it's just looking from back to front.

5 、 match

The retrieval returns the result of a string matching regular expression.

Syntax str.match (regexp) parameter regexp

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

Return value

If you use the g flag, all results that match the full regular expression are returned, but no capture group is returned.

If the g flag is not used, only the first full match and its associated capture group (Array) are returned. In this case, the returned project will have other properties as described below.

6 、 replace

Returns a new string that replaces some or all of the pattern matches with the replacement value (replacement). The pattern can be a string or a regular expression, and the replacement value can be a string or a callback function that is called every match. If pattern is a string, only the first match is replaced.

The original string does not change.

Grammar

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

Parameter regexp (pattern)

A RegExp object or its literal quantity. The content matched by the rule is replaced by the return value of the second parameter.

Substr (pattern)

A string to be replaced by newSubStr. It is treated as an entire string rather than a regular expression. Only the first match 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. Refer to the following using a string as a parameter.

Function (replacement)

A function used to create a new substring whose return value replaces the result matched by the first parameter. Refer to the following to specify a function as an argument.

Return value

One or all matches a new string that is replaced by an alternative pattern.

7 、 toLowerCase

Converts the string value to lowercase and returns

Syntax str.toLowerCase () returns a value

A new string that represents the call string converted to lowercase.

8 、 toUpperCase

Contrary to toLowerCase, turn to uppercase

9 、 substring

Returns a subset of a string from the start index to the end index, or from the start index to the end of the string.

Syntax str.substring (indexStart [, indexEnd]) parameter indexStart

The index of the first character to be intercepted, and the character of the index position as the first letter of the returned string.

IndexEnd

Optional. An integer between 0 and the length of a string in which characters indexed are not included in the intercepted string.

Return value

A new string that contains the specified part of the given string.

These are all the contents of this article entitled "what are the common methods of String in JavaScript?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report