Strings in Node.js
Master string handling in Node.js: single-line/multi-line strings, template literals, formatting, indexing, traversal, searching, slicing, comparing, replacing, ASCII conversions, and more.
Creating Strings
A string is a sequence of characters. In Node.js (JavaScript), strings can be created using single quotes, double quotes, or template literals.
1. Single Line Strings
Single-line strings can be written using single quotes or double quotes.
2. Multi-line Strings
Use template literals (backticks) to create multi-line strings naturally.
3. Template Literals / String Interpolation
Insert values into strings using `${}` inside backticks.
String Basics (Indexing, Length, Traversal)
Strings behave like arrays of characters. You can read characters using indexes and check string length using `.length`.
1. Accessing the Character at a Specific Index
Use `str[index]` to access a character. Index starts from 0.
2. Getting String Size (Length)
Use `.length` to get the number of characters in a string.
3. Traversing Through a String
Loop through each character using `for...of`.
Formatted Strings
Formatted strings are useful in CLI apps, logs, and reports. We use `padStart()` (pad left) and `padEnd()` (pad right) for alignment.
1. Formatted Output using padStart/padEnd
Print aligned columns like a table.
Comparing & Concatenating Strings
String comparison is used in searching, sorting, and validation. Concatenation builds larger strings from smaller pieces.
1. Comparing 2 Strings (Exact Match)
Use `===` to compare strings for exact equality.
2. Lexicographical Comparison (Dictionary Order)
Use `<`, `>` or `localeCompare()` for sorting and ordering.
3. Concatenating Strings (+) vs Template Literals
Both work, but template literals are usually cleaner.
4. concat() Method
Another way to join strings. Useful when chaining.
String Transformations
String transformations help in formatting and processing text.
1. Reversing a String (split → reverse → join)
Convert to array → reverse → join back.
2. Reversing a String (for loop using index)
Loop from end to start and build the reversed string.
3. Reversing a String (for...of prepend trick)
Reverse by adding each new char in front of the result string.
4. Uppercase of a String
Use `.toUpperCase()`.
5. Lowercase of a String
Use `.toLowerCase()`.
6. Repeat a String
Use `.repeat(n)`.
ASCII Conversions
ASCII/Unicode codes represent characters as numbers. JavaScript provides `.charCodeAt()` and `String.fromCharCode()`.
1. Convert String Character to ASCII Code
Use `charCodeAt(index)`.
2. Convert ASCII Code to Character
Use `String.fromCharCode(code)`.
Slicing, Substrings, Splitting & Trimming
These operations are extremely common for parsing emails, URLs, file names, and user input.
1. slice(a, b) (including a but excluding b)
Use `slice(a, b)` to get substring from a to b-1.
2. substring(a, b)
Works similar to slice, but handles negative values differently.
3. Split a String based on Specific String
Use `split(delimiter)` to get an array.
4. Trimming a String (Left, Right, Both)
Use `trimStart()`, `trimEnd()` and `trim()`. Pipes (|) in the output show where the string starts and ends so you can see the effect of trimming.
Searching & Replacing in Strings
Searching is used in validation, parsing, and filtering. Replacing is used in input cleanup and formatting.
1. Checking if String Has a Specific String/Character
Use `.includes(substring)`.
2. replace() (First Match Only)
`replace()` replaces only the first match unless regex is used with global flag.
3. replaceAll() (All Matches)
`replaceAll()` replaces every match of a substring.
4. replace() Using Global Regex (/pattern/g)
Regex with `g` flag replaces all matches (works like replaceAll, but supports patterns).
5. Finding the Index of Specific String
Use `.indexOf()`.
6. Finding the Last Index of Specific Character/String
Use `.lastIndexOf()`.
7. Check if a String Starts With a Specific String
Use `.startsWith(prefix)`.
8. Check if a String Ends With a Specific String
Use `.endsWith(suffix)`.