We'll explore in this part, some of the most commonly used string methods and provide examples to demonstrate how they work. So, let's get started!
1. String length
The first method we'll look at is
length. It allows us to find the number of characters in a string.
2. String slice()
Next up, we have the
slice() method. It extracts a portion of a string and returns a new string. By providing the starting index (inclusive) and the ending index (exclusive), we can select the desired substring:
3. String substring()
substring() method is quite similar to
slice(), but it handles negative indices differently.
4. String substr()
Now, we have the
substr() method is also similar to
slice(). It extracts a substring starting from a specific index and extending for the specified length:
5. String replace()
replace() method searches for a specified value or regular expression in a string and replaces it with a new value. By default, it replaces only the first case:
6. String replaceAll()
replaceAll() method replaces all occurrences of a specified value or regular expression:
7. String toUpperCase() and 8.String toLowerCase() These methods allow us to convert a string to either uppercase or lowercase:
9. String concat()
Finally, we have the
concat() method, which lets us combine multiple strings into a single string: