JavaScript provides a variety of methods to work with strings efficiently. Strings are a fundamental data type in JavaScript and are often used to store and manipulate text. This chapter will explain all the key string manipulation methods from basic to advanced, providing examples, detailed explanations, and insights so that you do not need any other resource to understand these concepts.
Definition: A string is a sequence of characters enclosed in quotes.
Strings can be created using single quotes ('), double quotes ("), or backticks (`).
console.log("helloword")let singleQuote = 'Hello, World!'; // Single quotes
let doubleQuote = "Hello, JavaScript!"; // Double quotes
let templateLiteral = `Hello, Developer!`; // Backticks (Template Literal)
console.log(singleQuote, doubleQuote, templateLiteral);
Hello, World! Hello, JavaScript! Hello, Developer!
Strings are immutable: You cannot change a character directly in a string. You can, however, create a new string with modifications.
These are the most commonly used string manipulation methods in JavaScript.
length PropertyThe length property returns the number of characters in a string.
let str = "JavaScript";
console.log(str.length); // Output: 10
charAt(index)The charAt method returns the character at a specified position (index) in a string.
let str = "Hello";
console.log(str.charAt(1)); // Output: e (Index starts from 0)
charCodeAt(index)The charCodeAt method returns the Unicode value of the character at the specified position.
let str = "A";
console.log(str.charCodeAt(0)); // Output: 65 (Unicode value of 'A')
toUpperCase() and toLowerCase()toUpperCase() converts a string to uppercase.
toLowerCase() converts a string to lowercase.
let str = "JavaScript";
console.log(str.toUpperCase()); // Output: JAVASCRIPT
console.log(str.toLowerCase()); // Output: javascript
trim()The trim method removes whitespace from both ends of a string.
let str = " Hello World! ";
console.log(str.trim()); // Output: "Hello World!"
JavaScript provides methods to extract parts of strings: slice, substring, and substr.
slice(start, end)The slice method extracts a section of a string and returns it as a new string.
start: The index to start extraction.
end: The index to stop extraction (not included).
let str = "Hello, JavaScript!";
console.log(str.slice(7, 17)); // Output: JavaScript
substring(start, end)The substring method works similarly to slice but does not accept negative indexes.
let str = "Hello, JavaScript!";
console.log(str.substring(7, 17)); // Output: JavaScript
Note: This method is deprecated and should not be used in modern JavaScript.
To search for specific substrings or patterns in a string, use the following methods:
indexOf()The indexOf method returns the position of the first occurrence of a substring. If not found, it returns -1.
let str = "Hello, World!";
console.log(str.indexOf("World")); // Output: 7
console.log(str.indexOf("JavaScript")); // Output: -1
lastIndexOf()The lastIndexOf method returns the position of the last occurrence of a substring.
let str = "Hello, Hello, World!";
console.log(str.lastIndexOf("Hello")); // Output: 7
includes()The includes method checks if a string contains a specific substring and returns true or false.
let str = "Hello, World!";
console.log(str.includes("World")); // Output: true
console.log(str.includes("JavaScript")); // Output: false
startsWith() and endsWith()startsWith() checks if a string starts with a specified substring.
endsWith() checks if a string ends with a specified substring.
let str = "Hello, JavaScript!";
console.log(str.startsWith("Hello")); // Output: true
console.log(str.endsWith("!")); // Output: true
replace()The replace method replaces the first occurrence of a substring with a new string.
let str = "Hello, World!";
let newStr = str.replace("World", "JavaScript");
console.log(newStr); // Output: Hello, JavaScript!
replaceAll()The replaceAll method replaces all occurrences of a substring.
let str = "Hello, World! World is big!";
let newStr = str.replaceAll("World", "JavaScript");
console.log(newStr); // Output: Hello, JavaScript! JavaScript is big!
split()The split method splits a string into an array of substrings based on a delimiter.
let str = "Hello,World,JavaScript";
let arr = str.split(",");
console.log(arr); // Output: ["Hello", "World", "JavaScript"]
join()The join method joins an array into a string with a specified separator.
let arr = ["Hello", "World", "JavaScript"];
let str = arr.join(" ");
console.log(str); // Output: Hello World JavaScript
String manipulation in JavaScript is essential for working with text-based data. Methods such as slice, replace, split, and join provide powerful tools for handling strings efficiently. By mastering these methods, you can perform complex text operations with ease and precision. This chapter covered everything from basic concepts to advanced techniques, ensuring you have a complete understanding of string manipulation. Happy coding !❤️
