JavaScript Guide: Strings and Commonly Used Methods
Strings are one of the most commonly used data types in JavaScript. A string is a sequence of characters used to represent text, and JavaScript provides a rich set of built-in methods to manipulate strings efficiently.
Understanding JavaScript Strings
Strings in JavaScript are immutable, meaning that once a string is created, it cannot be altered directly. Any method applied to a string will return a new string rather than modifying the original.
Here, we discuss 15 commonly used string methods in JavaScript, their internal working, time complexity, and space complexity.
charAt(index)
Returns the character at the specified index in a string.
- Internal Working: Looks up the character at the specified index.
- Time Complexity: O(1)
- Space Complexity: O(1)
concat(str1, str2, ...)
Combines two or more strings into one.
- Internal Working: Joins the specified strings to the original.
- Time Complexity: O(n), where n is the length of the strings being concatenated.
- Space Complexity: O(n)
includes(substring)
Checks if the string contains the specified substring.
- Internal Working: Uses a search algorithm to locate the substring.
- Time Complexity: O(n)
- Space Complexity: O(1)
indexOf(substring)
Returns the first index where the substring appears, or -1 if not found.
- Internal Working: Scans from the beginning of the string until the substring is found.
- Time Complexity: O(n)
- Space Complexity: O(1)
lastIndexOf(substring)
Returns the last index where the substring appears, or -1 if not found.
- Internal Working: Scans from the end of the string until the substring is found.
- Time Complexity: O(n)
- Space Complexity: O(1)
replace(substring, newSubstring)
Replaces the first occurrence of a specified substring.
- Internal Working: Searches for the substring and constructs a new string with the replacement.
- Time Complexity: O(n)
- Space Complexity: O(n)
replaceAll(substring, newSubstring)
Replaces all occurrences of a specified substring.
- Internal Working: Searches for all occurrences and constructs a new string.
- Time Complexity: O(n)
- Space Complexity: O(n)
slice(start, end)
Extracts a section of a string between the specified indices.
- Internal Working: Creates a new string from the specified section.
- Time Complexity: O(n)
- Space Complexity: O(n)
split(separator)
Divides the string into an array of substrings based on a specified separator.
- Internal Working: Splits at each occurrence of the separator and constructs an array.
- Time Complexity: O(n)
- Space Complexity: O(n)
substring(start, end)
Extracts characters from the start index to, but not including, the end index.
- Internal Working: Creates a new substring.
- Time Complexity: O(n)
- Space Complexity: O(n)
toLowerCase()
Converts all characters to lowercase.
- Internal Working: Checks each character and converts uppercase to lowercase.
- Time Complexity: O(n)
- Space Complexity: O(n)
toUpperCase()
Converts all characters to uppercase.
- Internal Working: Checks each character and converts lowercase to uppercase.
- Time Complexity: O(n)
- Space Complexity: O(n)
trim()
Removes whitespace from both ends of a string.
- Internal Working: Traverses the start and end of the string, identifying whitespace to remove.
- Time Complexity: O(n)
- Space Complexity: O(n)
trimStart()
Removes whitespace from the start of a string.
- Internal Working: Traverses the start of the string to remove leading whitespace.
- Time Complexity: O(n)
- Space Complexity: O(n)
trimEnd()
Removes whitespace from the end of a string.
- Internal Working: Traverses the end of the string to remove trailing whitespace.
- Time Complexity: O(n)
- Space Complexity: O(n)
This guide provides an understanding of each method’s purpose, internal working, and the time and space complexity involved. These foundational methods are essential for string manipulation tasks in JavaScript.