JavaScript String Methods

JavaScript strings are sequences of characters. They are primitive data types, which means that they are immutable and cannot be changed. However, JavaScript provides a number of string methods that allow you to manipulate strings in a variety of ways.

Searching for substrings

One of the most common tasks that you need to perform with strings is to search for substrings. This can be done using the following methods:

Search(): This method searches for a substring in a string using a regular expression. It returns the index of the first occurrence of the substring, or -1 if the substring is not found.

IndexOf(): This method searches for a substring in a string using a literal string. It returns the index of the first occurrence of the substring, or -1 if the substring is not found.

LastIndexOf(): This method searches for a substring in a string using a literal string. It returns the index of the last occurrence of the substring, or -1 if the substring is not found.

Includes(): This method checks if a string contains a substring. It returns true if the string contains the substring, or false if it does not.

Extracting substrings

Once you have found a substring in a string, you can extract it using the following methods:

Slice(): This method extracts a substring from a string, starting at a specified index and ending at another specified index. The extracted substring is returned as a new string.

Substring(): This method is similar to slice(), but it does not allow the end index to be greater than the length of the string.

Substr(): This method extracts a substring from a string, starting at a specified index and ending at a specified length. The extracted substring is returned as a new string.

Manipulating strings

In addition to searching for and extracting substrings, you can also use JavaScript string methods to manipulate strings in other ways. For example, you can use the following methods to:

Concat(): This method concatenates two or more strings together.

ToUpperCase(): This method converts a string to all uppercase letters.

ToLowerCase(): This method converts a string to all lowercase letters.

Trim(): This method removes whitespace characters from the beginning and end of a string.

Split(): This method splits a string into an array of strings, using a specified delimiter.

Replace(): This method replaces all occurrences of a substring in a string with another substring.

Using string methods

Here are some examples of how to use JavaScript string methods:

// Search for a substring in a string
const str = "Hello, world!";
const index = str.indexOf("world");
console.log(index);
// 6
// Extract a substring from a string
const substring = str.slice(6);
console.log(substring);
// “world!”
// Concatenate two strings together
const str1 = "Hello, ";
const str2 = "world!";
const newStr = str1.concat(str2);
console.log(newStr);
// “Hello, world!”
// Convert a string to all uppercase letters
const upperStr = str.toUpperCase();
console.log(upperStr);
// “HELLO, WORLD!”
// Remove whitespace characters from the beginning and end of a string
const trimmedStr = str.trim();
console.log(trimmedStr);
// “Hello, world!”
// Split a string into an array of strings, using a specified delimiter
const arr = str.split(" ");
console.log(arr);
// [“Hello”, “world!”]
// Replace all occurrences of a substring in a string with another substring
const replacedStr = str.replace("world", "universe");
console.log(replacedStr);
// “Hello, universe!”

Conclusion

JavaScript string methods provide a powerful way to manipulate strings. By understanding and using these methods, you can write more efficient and effective JavaScript code.