whenever life put's you in a tough situtation, never say why me! but, try me!

Chapter 6: String Methods

Strings are a fundamental part of JavaScript and represent a sequence of characters. This chapter covers various string methods, string interpolation, and template literals to help you manipulate and work with strings effectively.

String Methods

JavaScript provides several built-in methods for working with strings. These methods allow you to perform operations such as searching, replacing, and manipulating string data.

Common String Methods:

charAt()

The charAt() method returns the character at a specified index in a string.

Syntax:

string.charAt(index);

Example:

let str = "Hello";
console.log(str.charAt(1)); // Output: 'e'

concat()

The concat() method joins two or more strings and returns a new string.

Syntax:

string.concat(string2, string3, ..., stringN)

Example:

let str1 = "Hello";
let str2 = "World";
let result = str1.concat(" ", str2);

console.log(result); // Output: 'Hello World'

includes()

The includes() method determines whether a string contains a specified substring.

Syntax:

string.includes(searchString, position);

Example:

let str = "Hello World";
console.log(str.includes("World")); // Output: true
console.log(str.includes("world")); // Output: false (case-sensitive)

indexOf()

The indexOf() method returns the index of the first occurrence of a specified substring. Returns -1 if the substring is not found.

Syntax:

string.indexOf(searchValue, fromIndex);

Example:

let str = "Hello World";
console.log(str.indexOf("World")); // Output: 6
console.log(str.indexOf("world")); // Output: -1 (case-sensitive)

lastIndexOf()

The lastIndexOf() method returns the index of the last occurrence of a specified substring. Returns -1 if the substring is not found.

Syntax:

string.lastIndexOf(searchValue, fromIndex);

Example:

let str = "Hello World World";
console.log(str.lastIndexOf("World")); // Output: 12

replace()

The replace() method replaces a specified substring or pattern with a new substring. You can use a string or a regular expression as the search value.

Syntax:

string.replace(searchValue, newValue);

Example:

let str = "Hello World";
let result = str.replace("World", "Universe");

console.log(result); // Output: 'Hello Universe'

slice()

The slice() method extracts a section of a string and returns it as a new string.

Syntax:

string.slice(beginIndex, endIndex);

Example:

let str = "Hello World";
console.log(str.slice(0, 5)); // Output: 'Hello'

split()

The split() method splits a string into an array of substrings based on a specified delimiter.

Syntax:

string.split(separator, limit);

Example:

let str = "Hello World";
let parts = str.split(" ");

console.log(parts); // Output: ['Hello', 'World']

toLowerCase()

The toLowerCase() method converts all characters in a string to lowercase.

Syntax:

string.toLowerCase();

Example:

let str = "Hello World";
console.log(str.toLowerCase()); // Output: 'hello world'

toUpperCase()

The toUpperCase() method converts all characters in a string to uppercase.

Syntax:

string.toUpperCase();

Example:

let str = "Hello World";
console.log(str.toUpperCase()); // Output: 'HELLO WORLD'

trim()

The trim() method removes whitespace from both ends of a string.

Syntax:

string.trim();

Example:

let str = "   Hello World   ";
console.log(str.trim()); // Output: 'Hello World'

String Interpolation

String interpolation is a feature that allows you to embed expressions inside string literals. This feature is primarily used with template literals.

Example using Template Literals:

let name = "John";
let age = 30;
let greeting = `Hello, my name is ${name} and I am ${age} years old.`;

console.log(greeting); // Output: 'Hello, my name is John and I am 30 years old.'

String as Character Array

In JavaScript, strings can be treated as arrays of characters. While strings themselves are not arrays, you can access individual characters by their index.

Example:

let str = "Hello";
console.log(str[0]); // Output: 'H'
console.log(str[1]); // Output: 'e'

Converting a String to an Array of Characters:

let str = "Hello";
let charArray = Array.from(str);

console.log(charArray); // Output: ['H', 'e', 'l', 'l', 'o']

Template Literals

Template literals are a way to create strings with embedded expressions. They are enclosed by backticks (`) instead of single or double quotes.

Features of Template Literals:

  • String Interpolation: Embed expressions within a string using ${expression}.
  • Multiline Strings: Create strings that span multiple lines without needing escape sequences.

Example:

let name = "John";
let age = 30;
let multiline = `
Hello, my name is ${name}.
I am ${age} years old.
`;

console.log(multiline);
// Output:
// Hello, my name is John.
// I am 30 years old.