These are some most Important String Methods and their examples:
1. length() : The length method returns the length of a string.
Example:
Output:
2. slice(start, end) : slice() extract a part of a string and returns the extracted part in a new string.
Example:
Output:
3. substring(start, end) : substring() is similar to slice() function. The difference is substring cannot accept negative indexes.
Example:
Output:
4. substr(start, length) : substr() is similar to slice(). The difference is, the second parameter specifies the length.
Example:
Output:
5. toUppercase() : toUppercase() simply converts a string into uppercase.
Example:
Output:
6. toLowercase() : toLowercase() simply converts a string into lowercase.
Example:
Output:
7. concat() : concat() joins two or more strings. For example, if you want to join your first name and last name, you can use concat method to join.
Example:
Output:
8. trim() : trim() method remove whitespaces from both side of a string. For example, if you are copying a text from another websites, there might be an unnecessary white spaces which you want to remove. You can use trim function to remove them.
Example:
Output:
9. replace() : replace() method replace a specified value with another value in a string and return extracted part.
Example:
Output:
10. split() : A string can be converted into an array using split() method.
Example:
Output: