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.
js逗号分割的字符串转数组 `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 specifie
d 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.
`toLowerCase()`: Returns a new string that contains the original string converted to lowercase.
`trim()`: Returns a new string that removes leading and trailing whitespace from the original string.
`split(separator, limit)`: Returns an array of substrings that are separated by the specified separator. The optional limit parameter specifies the maximum number of substrings to return.
`concat(string1, string2, ...)`: Returns a new string that is the concatenation of the original string and the specified strings.
`includes(substring)`: Returns true if the string contains the specified substring, otherwise false.
`startsWith(substring)`: Returns true if the string starts with the specified substring, otherwise false.
`endsWith(substring)`: Returns true if the string ends with the specified substring, otherwise false.
These are just a few of the many string methods that are available in JavaScript. By understanding and using these methods, you can effectively manipulate and process strings in your code.
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论