JavaScript String Methods
Strings are fundamental to programming. JavaScript provides powerful methods for creating, searching, and manipulating strings.
📝 What are Strings?
A string is a sequence of characters used to represent text. In JavaScript, strings are immutable primitive values.
String Characteristics:
- Immutable (cannot be changed once created)
- Zero-based indexing for characters
- Can be created with '', "", or ``
- Have a length property
- Many built-in methods for manipulation
💡 String Creation Methods
// Single quotes
let s1 = 'Hello';
// Double quotes
let s2 = "World";
// Backticks (template literals)
let s3 = `Hello ${s2}`;
// String constructor
let s4 = new String("String object");
// String.fromCharCode()
let s5 = String.fromCharCode(72, 101, 108, 108, 111);Escape Sequences:
\\' Single quote\\" Double quote\\\\ Backslash\\n New line\\t Tab
🔧 Basic String Methods
Let's explore basic string creation, access, and common methods.
Access
str[index] str.charAt(index)
Case
str.toUpperCase() str.toLowerCase()
Check
str.startsWith() str.endsWith() str.includes()
Repeat
str.repeat(count)
🔍 String Search Methods
JavaScript provides various methods for searching within strings.
Position Search
indexOf()- First occurrencelastIndexOf()- Last occurrencesearch()- Regex position
Match Search
match()- Regex matchesmatchAll()- All regex matchesincludes()- Check inclusion
Character Check
charAt()- Character at indexcharCodeAt()- Unicode at indexcodePointAt()- Full Unicode
✂️ String Extraction Methods
Extract parts of strings using various methods.
📌 slice() vs substring() vs substr():
- slice(start, end) - Can use negative indexes, most flexible
- substring(start, end) - Swaps if start > end, no negative indexes
- substr(start, length) - Deprecated, uses length instead of end
- Recommendation: Use
slice()for most cases
✨ String Modification Methods
Modify strings by replacing, trimming, padding, and concatenating.
Trim
trim() trimStart() trimEnd()
Replace
replace() replaceAll() replace(regex)
Pad
padStart() padEnd()
💎 Template Literals (ES6)
Template literals provide an easy way to create strings with embedded expressions and multi-line support.
🎯 Template Literal Features:
- String interpolation - Embed expressions with
{$ {expression}} - Multi-line strings - No need for
\\nor concatenation - Tagged templates - Process template literals with functions
- Raw strings - Access raw template string with
String.raw - Nested templates - Template literals inside expressions
🎮 String Practice Exercises
Implement the string manipulation functions below.
💪 Additional Challenges:
- Implement URL slug generator
- Create string compression (run-length encoding)
- Build a simple markdown parser
- Implement search highlighting function
- Create a function to generate random strings
📋 Quick Reference
Common Methods:
length- String lengthtoUpperCase()/toLowerCase()- Case changetrim()- Remove whitespacesplit()- Split into arrayslice()- Extract substring
Search Methods:
indexOf()/lastIndexOf()- Find positionincludes()- Check inclusionstartsWith()/endsWith()- Check start/endmatch()/matchAll()- Regex matchingsearch()- Regex position