JavaScript provides many built-in string methods for manipulation and searching.
Extraction methods:
Slice extracts a portion using start and end indices. Accepts negative indices to count from end. Most versatile extraction method.
Substring similar to slice but does not accept negative indices. Swaps arguments if start is greater than end.
Substr extracts from start index with specified length. Deprecated and should be avoided.
Search methods:
IndexOf finds first occurrence and returns index or minus 1. LastIndexOf searches from end.
Includes returns true or false if substring exists. Modern and cleaner than indexOf.
StartsWith checks if string begins with substring. EndsWith checks if string ends with substring.
Modification methods:
Replace replaces first occurrence. ReplaceAll replaces all occurrences.
ToUpperCase and toLowerCase change case.
Trim removes whitespace from both ends. TrimStart and trimEnd for one side only.
Split divides string into array based on separator.
Other useful methods:
Repeat repeats string specified number of times.
PadStart and padEnd add padding to reach target length.
CharAt accesses character at index.
All methods return new strings without modifying the original because strings are immutable.
Understanding these methods is essential for string manipulation in real applications.
Example code
const str = 'JavaScript Programming';
// EXTRACTION
// slice(start, end)
console.log(str.slice(0, 10)); // 'JavaScript'
console.log(str.slice(-11)); // 'Programming'
console.log(str.slice(4, -1)); // 'Script Programmin'
// substring(start, end)
console.log(str.substring(0, 10)); // 'JavaScript'
console.log(str.substring(4, 10)); // 'Script'
// SEARCH
// indexOf(searchValue, startPosition)
console.log(str.indexOf('a')); // 1 (first 'a')
console.log(str.indexOf('a', 2)); // 3 (search from index 2)
console.log(str.indexOf('Python')); // -1 (not found)
// includes(searchValue)
console.log(str.includes('Script')); // true
console.log(str.includes('Python')); // false
// startsWith(), endsWith()
console.log(str.startsWith('Java')); // true
console.log(str.endsWith('ing')); // true
// MODIFICATION
// replace(search, replacement)
const text = 'I like cats. Cats are great.';
console.log(text.replace('cats', 'dogs'));
// 'I like dogs. Cats are great.' (only first)
console.log(text.replaceAll('cats', 'dogs'));
// 'I like dogs. dogs are great.' (case-sensitive!)
console.log(text.replace(/cats/gi, 'dogs'));
// 'I like dogs. dogs are great.' (case-insensitive)
// toUpperCase(), toLowerCase()
console.log(str.toUpperCase()); // 'JAVASCRIPT PROGRAMMING'
console.log(str.toLowerCase()); // 'javascript programming'
// trim(), trimStart(), trimEnd()
const spaced = ' Hello ';
console.log(spaced.trim()); // 'Hello'
console.log(spaced.trimStart()); // 'Hello '
console.log(spaced.trimEnd()); // ' Hello'
// split(separator, limit)
console.log(str.split(' ')); // ['JavaScript', 'Programming']
console.log('a,b,c'.split(','));// ['a', 'b', 'c']
console.log('hello'.split('')); // ['h','e','l','l','o']
// OTHER METHODS
// repeat(count)
console.log('Ha'.repeat(3)); // 'HaHaHa'
// padStart(length, padString)
console.log('5'.padStart(3, '0')); // '005'
console.log('5'.padEnd(3, '0')); // '500'
// charAt(index)
console.log(str.charAt(0)); // 'J'
console.log(str.charAt(100));// '' (empty string)
// concat(str1, str2, ...)
console.log('Hello'.concat(' ', 'World')); // 'Hello World'