Introduction
In this article, we will cover String Methods in JavaScript. Before we start, Let's understand the objective of this demonstration, which tells what exactly will be covered in this article.
- charAt(index)
- concat(str1, str2, ..., strN)
- includes(searchString, position)
- indexOf(searchValue, fromIndex)
- lastIndexOf(searchValue, fromIndex)
- replace(searchValue, replaceValue)
- slice(startIndex, endIndex)
- split(separator, limit)
- toLowerCase()
- toUpperCase()
- trim()
- match()
- repeat()
- startsWith()
- substr()
So, Let's get started.
What are String Methods?
In JavaScript, a string method is a built-in function that can be called on a string object to perform operations on that string. String methods allow developers to manipulate strings, extract information from them, and perform other useful tasks.
charAt(index)
Returns the character at the specified index.
const str = "Hello World";
console.log(str.charAt(0));
// Output: H
concat(str1, str2, ..., strN)
Concatenates two or more strings.
const str1 = "Hello";
const str2 = "World";
console.log(str1.concat(" ", str2));
//Output: "Hello World"
includes(searchString, position)
Checks whether a string contains a specified substring. Returns true or false.
const str = "Hello World";
console.log(str.includes("World"));
//Output: true
console.log(str.includes("world"));
//Output: false
indexOf(searchValue, fromIndex)
Returns the index of the first occurrence of a specified value in a string.
const str = "Hello World";
console.log(str.indexOf("o"));
//Output: 4
console.log(str.indexOf("l", 4));
//Output: 9
lastIndexOf(searchValue, fromIndex)
Returns the index of the last occurrence of a specified value in a string.
const str = "Hello World";
console.log(str.lastIndexOf("o"));
//Output: 7
console.log(str.lastIndexOf("l", 4));
//Output: 3
replace(searchValue, replaceValue)
Replaces a specified value with another value in a string.
const str = "Hello World";
console.log(str.replace("World", "Universe"));
//Output: "Hello Universe"
slice(startIndex, endIndex)
Extracts a part of a string and returns a new string.
const str = "Hello World";
console.log(str.slice(0, 5));
//Output: "Hello"
console.log(str.slice(6));
//Output: "World"
split(separator, limit)
Splits a string into an array of substrings based on a specified separator.
const str = "Hello World";
console.log(str.split(" "));
//Output: ["Hello","World"]
toLowerCase()
Converts a string to lowercase.
onst str = "Hello World";
console.log(str.toLowerCase());
//Output: "hello world"
toUpperCase()
Converts a string to uppercase.
const str = "Hello World";
console.log(str.toUpperCase());
//Output: "HELLO WORLD"
trim()
Removes whitespace from both ends of a string.
const str = " Hello World ";
console.log(str.trim());
//Output: "Hello World"
match()
Searches a string for a match against a regular expression.
const str = "Hello World";
console.log(str.match(/o/g));
//Output: ["o","o"]
repeat()
Returns a new string with a specified number of copies of an existing string.
const str = "Hello";
console.log(str.repeat(3));
//Output: "HelloHelloHello"
startsWith()
Checks whether a string starts with a specified substring.
const str = "Hello World";
console.log(str.startsWith("Hello"));
//Output: true
substr()
Extracts a specified number of characters from a string, starting at a specified index.
const str = "Hello World";
console.log(str.substr(6, 5));
//Output: "World"
Summary
In this article, I have tried to cover String Method in Javascript.