JavaScript String Methods


Strings are texts that aid in the storage of data that may be expressed. A JavaScript string contains a string of characters such as "Welcome To WebTutor." A string is any text enclosed in double or single quotations.

Syntax

String indexes begin at 0. The first character is in place 0, the second in position 1, and so on. We may call any of JavaScript's pre-defined methods since it automatically transforms string primitives to objects.


1. String length

A string's length is returned by the length property.

Example

Preview

2. String slice()

Extracts a section of a string and returns a new string.

Example

Preview

3. String substring()

Extracts a section of a string between two specified indices and returns a new string.

Example

Preview

4. String substr()

Extracts a section of a string starting from a specified index and returns a new string.

Example

Preview

5. String replace()

Searches a string for specified characters and replaces them with new characters.

Example

Preview

6. String replaceAll()

In place of the string to be replaced, you can supply a regular expression using the replaceAll() function. Must be global flag (g), JavaScript string set if parameter is regular expression; Otherwise, a TypeError is raised.

Example

Preview

7. String toUpperCase()

Converts a string to uppercase.

Example

Preview

8. String toLowerCase()

Converts a string to lowercase.

Example

Preview

9. String concat()

Joins two or more strings and returns a new string.

Example

Preview

10. String trim()

Removes whitespace from both ends of a string.

Example

Preview

11. String trimStart()

Removes whitespace from the beginning of a string.

Example

Preview

12. String trimEnd()

Removes whitespace from the end of a string.

Example

Preview

13. String padStart()

Pads the beginning of a string with specified characters.

Example

Preview

14. String padEnd()

Pads the end of a string with specified characters.

Example

Preview

15. String charAt()

Returns the character at a specified index in a string.

Example

Preview

16. String charCodeAt()

Returns the Unicode of the character at a specified index in a string.

Example

Preview

17. String split()

Splits a string into an array of substrings based on specified characters.

Example

Preview

18. String search()

Searches a string for specified characters and returns the index of the first occurrence.

Example

Preview

19. String match()

Searches a string for specified characters and returns an array of matches.

Example

Preview

20. String matchAll()

Returns an iterator of all matches of a regular expression in a string.

Example

Preview

21. String includes()

Checks whether a string contains specified characters or not.

Example

Preview

22. String startsWith()

Checks whether a string starts with specified characters or not.

Example

Preview

23. String endsWith()

Checks whether a string ends with specified characters or not.

Example

Preview

24. String indexOf()

The index of (position of) the first instance of a string in a string is returned by the indexOf() function.

Example

Preview

25. String lastIndexOf()

The lastIndexOf() function returns the string's string's index for the final instance of the provided text.

Example

Preview