admin 管理员组

文章数量: 1087139


2024年4月13日发(作者:general怎么读英语)

js 中 字符串的方法

## JavaScript String Methods.

Strings are a fundamental data type in JavaScript and

they come with a wide range of built-in methods that allow

you to perform various operations on them. Here are some of

the most commonly used string methods:

`length`: Returns the length of the string.

`charAt(index)`: Returns the character at the specified

index in the string.

`charCodeAt(index)`: Returns the Unicode code point of

the character at the specified index in the string.

`indexOf(substring, startIndex)`: Returns the index of

the first occurrence of the specified substring in the

string, starting from the specified start index.

`lastIndexOf(substring, startIndex)`: Returns the index

of the last occurrence of the specified substring in the

string, starting from the specified start index.

`slice(start, end)`: Returns a new string that contains

a portion of the original string, starting at the specified

start index and ending at the specified end index.

`substring(start, end)`: Similar to `slice`, but it

does not accept negative indices.

`substr(start, length)`: Returns a new string that

contains a portion of the original string, starting at the

specified start index and continuing for the specified

length.

`replace(substring, newSubstring)`: Returns a new

string that replaces all occurrences of the specified

substring with the specified new substring.

`toUpperCase()`: Returns a new string that contains the

original string converted to uppercase.


本文标签: 方法 字符串