JavaScript Essentials - Strings
Manipulating and using strings in JavaScript
Strings are used to store and manipulate text. This tutorial covers how to create, manipulate, and use strings in JavaScript.
Key Points:
- Strings are used to store text.
- JavaScript provides many built-in methods for string manipulation.
- Understanding how to work with strings is essential for text processing and manipulation in your programs.
Creating Strings
Strings can be created using single quotes, double quotes, or backticks for template literals. Here is an example of each:
// Using single quotes
var singleQuoteString = 'Hello, World!';
// Using double quotes
var doubleQuoteString = "Hello, World!";
// Using backticks for template literals
var templateLiteralString = `Hello, World!`;
console.log(singleQuoteString); // Output: Hello, World!
console.log(doubleQuoteString); // Output: Hello, World!
console.log(templateLiteralString); // Output: Hello, World!
String Properties and Methods
JavaScript provides many built-in properties and methods for string manipulation. Here are some common ones:
length
- Returns the length of the string.charAt()
- Returns the character at a specified index.indexOf()
- Returns the index of the first occurrence of a specified value.lastIndexOf()
- Returns the index of the last occurrence of a specified value.slice()
- Extracts a part of a string and returns it as a new string.substring()
- Extracts characters from a string between two specified indices.substr()
- Extracts a part of a string, beginning at a specified index and extending for a given number of characters.replace()
- Replaces a specified value with another value in a string.toUpperCase()
- Converts a string to uppercase letters.toLowerCase()
- Converts a string to lowercase letters.split()
- Splits a string into an array of substrings.trim()
- Removes whitespace from both ends of a string.
Examples of String Methods
var str = 'Hello, World!';
// length
console.log(str.length); // Output: 13
// charAt
console.log(str.charAt(0)); // Output: H
// indexOf
console.log(str.indexOf('o')); // Output: 4
// lastIndexOf
console.log(str.lastIndexOf('o')); // Output: 8
// slice
console.log(str.slice(0, 5)); // Output: Hello
// substring
console.log(str.substring(0, 5)); // Output: Hello
// substr
console.log(str.substr(0, 5)); // Output: Hello
// replace
console.log(str.replace('World', 'JavaScript')); // Output: Hello, JavaScript!
// toUpperCase
console.log(str.toUpperCase()); // Output: HELLO, WORLD!
// toLowerCase
console.log(str.toLowerCase()); // Output: hello, world!
// split
console.log(str.split(', ')); // Output: ["Hello", "World!"]
// trim
var strWithWhitespace = ' Hello, World! ';
console.log(strWithWhitespace.trim()); // Output: Hello, World!
Template Literals
Template literals allow for easier string interpolation and multi-line strings. They are enclosed by backticks (`
) instead of single or double quotes. Here is an example:
var name = 'Alice';
var greeting = `Hello, ${name}!`;
console.log(greeting); // Output: Hello, Alice!
var multiLineString = `This is a string
that spans multiple
lines.`;
console.log(multiLineString);
// Output:
// This is a string
// that spans multiple
// lines.
Escape Characters
Escape characters are used to insert special characters in a string. Here are some common escape characters:
\n
- New line\t
- Tab\'
- Single quote\"
- Double quote\\
- Backslash
var str = 'Hello,\nWorld!';
console.log(str);
// Output:
// Hello,
// World!
var str = 'She said, \"Hello!\"';
console.log(str); // Output: She said, "Hello!"
Summary
In this tutorial, you learned about manipulating and using strings in JavaScript. You explored creating strings, string properties and methods, template literals, and escape characters. Understanding how to work with strings is essential for text processing and manipulation in your JavaScript programs.