What is a String?
*The sequence of one or more characters enclosed within quotation marks is called a string.
*The quotation can be single quotes '' or double quotes " " or backtick ``.
And, the sequence of characters can be alphabets, numbers, symbols, etc.
String methods
1.charAt(index)
*The charAt() returns the character at a specified index in a string.
*The very first character of the string has an index of 0, the second character has index 1, and so on...
Example
let text = "HELLO";
let char = text.charAt(0);
console.log(char)// Returns "H"
2.length
*This property returns the number of characters in a string.
Example
let text = "HELLO";
let length = text.length;
console.log(length) // Returns 5
3.slice(start, end)
*Extracts a section of a string and returns it as a new string. The start index is inclusive, while the end index is exclusive.
Example
let text = "HELLO WORLD";
let part = text.slice(0, 5);
console.log(part) // Returns "HELLO"
4.substring(start, end)
*Similar to slice(), but treats negative indices as 0. It extracts characters between two specified indices.
Example
let text = "HELLO WORLD";
let part = text.substring(0, 5);
console.log(part)// Returns "HELLO"
5.toUpperCase()
*Converts all characters in a string to uppercase.
Example
let text = "hello";
let upper = text.toUpperCase();
console.log(upper)// Returns "HELLO"
6.toLowerCase()
*Converts all characters in a string to lowercase.
Example
let text = "HELLO";
let lower = text.toLowerCase();
console.log(lower)// Returns "hello"
7.trim()
*Removes whitespace from both ends of a string.
Example
let text = " hello ";
let trimmed = text.trim();
console.log(trimmed) // Returns "hello"
8.concat()
*Joins two or more strings and returns a new string.
Example
let text1 = "Hello";
let text2 = "World";
let combined = text1.concat(" ", text2);
console.log(combined) // Returns "Hello World"
9.indexOf(substring)
*Returns the index of the first occurrence of a specified substring. Returns -1 if not found.
Example
let text = "HELLO WORLD";
let index = text.indexOf("O");
console.log(index)// Returns 4
10.replace(searchValue, newValue)
*Replaces the first occurrence of a specified value with a new value.
Example
let text = "HELLO WORLD";
let newText = text.replace("WORLD", "EVERYONE");
console.log(newText)// Returns "HELLO EVERYONE"
11.replaceAll(searchValue, newValue)
*Replaces all occurrences of a specified value with a new value.
Example
let text = "HELLO WORLD WORLD";
let newText = text.replaceAll("WORLD", "EVERYONE");
console.log(newText)// Returns "HELLO EVERYONE EVERYONE"
12.split(separator)
*Splits a string into an array of substrings based on a specified separator.
Example
let text = "HELLO WORLD";
let parts = text.split(" ");
console.log(parts)// Returns ["HELLO", "WORLD"]
13.join(separator)
*Joins the elements of an array into a string, using a specified separator.
Example
let array = ["HELLO", "WORLD"];
let joined = array.join(" ");
console.log(joined)// Returns "HELLO WORLD"
14.startsWith(searchString)
*Checks if the string starts with the specified string.
Example
let text = "HELLO WORLD";
let starts = text.startsWith("HELLO");
console.log(starts)// Returns true
15.endsWith(searchString)
*Checks if the string ends with the specified string.
Example
let text = "HELLO WORLD";
let ends = text.endsWith("WORLD");
console.log(ends)// Returns true
16.includes(searchString)
*Checks if the string contains the specified substring.
Example
let text = "HELLO WORLD";
let includes = text.includes("LO");
console.log(includes)// Returns true
Top comments (0)