Home >Web Front-end >JS Tutorial >Comprehensive Guide to String Manipulation in JavaScript
String manipulation is a core aspect of working with text in JavaScript. JavaScript provides a rich set of built-in methods and techniques for handling and transforming strings.
Strings in JavaScript can be created using single quotes ('), double quotes ("), or backticks (` for template literals).
Example:
const single = 'Hello'; const double = "World"; const template = `Hello, ${double}!`; // Using template literals console.log(template); // Output: Hello, World!
The .length property returns the number of characters in a string.
const text = "JavaScript"; console.log(text.length); // Output: 10
You can access individual characters using bracket notation or the .charAt() method.
const str = "Hello"; console.log(str[0]); // Output: H console.log(str.charAt(1)); // Output: e
const text = "JavaScript"; console.log(text.toUpperCase()); // Output: JAVASCRIPT console.log(text.toLowerCase()); // Output: javascript
const text = "JavaScript is awesome!"; console.log(text.indexOf("is")); // Output: 11 console.log(text.lastIndexOf("a")); // Output: 3
const text = "Hello, world!"; console.log(text.includes("world")); // Output: true console.log(text.startsWith("Hello")); // Output: true console.log(text.endsWith("!")); // Output: true
const text = "JavaScript"; console.log(text.slice(0, 4)); // Output: Java console.log(text.substring(4, 10)); // Output: Script console.log(text.substr(4, 6)); // Output: Script
The .split(delimiter) method splits a string into an array of substrings.
const single = 'Hello'; const double = "World"; const template = `Hello, ${double}!`; // Using template literals console.log(template); // Output: Hello, World!
const text = "JavaScript"; console.log(text.length); // Output: 10
const str = "Hello"; console.log(str[0]); // Output: H console.log(str.charAt(1)); // Output: e
You can reverse a string by converting it to an array, reversing the array, and joining it back into a string.
const text = "JavaScript"; console.log(text.toUpperCase()); // Output: JAVASCRIPT console.log(text.toLowerCase()); // Output: javascript
The .repeat(count) method repeats a string multiple times.
const text = "JavaScript is awesome!"; console.log(text.indexOf("is")); // Output: 11 console.log(text.lastIndexOf("a")); // Output: 3
const text = "Hello, world!"; console.log(text.includes("world")); // Output: true console.log(text.startsWith("Hello")); // Output: true console.log(text.endsWith("!")); // Output: true
Template literals provide a more readable and concise way to create dynamic strings.
const text = "JavaScript"; console.log(text.slice(0, 4)); // Output: Java console.log(text.substring(4, 10)); // Output: Script console.log(text.substr(4, 6)); // Output: Script
By mastering these techniques, you'll be well-equipped to handle complex text operations in your JavaScript applications.
Hi, I'm Abhay Singh Kathayat!
I am a full-stack developer with expertise in both front-end and back-end technologies. I work with a variety of programming languages and frameworks to build efficient, scalable, and user-friendly applications.
Feel free to reach out to me at my business email: kaashshorts28@gmail.com.
The above is the detailed content of Comprehensive Guide to String Manipulation in JavaScript. For more information, please follow other related articles on the PHP Chinese website!