*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.
*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...
let text = "HELLO";
let char = text.charAt(0);
console.log(char)// Returns "H"
*This property returns the number of characters in a string.
let text = "HELLO";
let length = text.length;
console.log(length) // Returns 5
*Extracts a section of a string and returns it as a new string. The start index is inclusive, while the end index is exclusive.
let text = "HELLO WORLD";
let part = text.slice(0, 5);
console.log(part) // Returns "HELLO"
*Similar to slice(), but treats negative indices as 0. It extracts characters between two specified indices.
let text = "HELLO WORLD";
let part = text.substring(0, 5);
console.log(part)// Returns "HELLO"
*Converts all characters in a string to uppercase.
let text = "hello";
let upper = text.toUpperCase();
console.log(upper)// Returns "HELLO"
*Converts all characters in a string to lowercase.
let text = "HELLO";
let lower = text.toLowerCase();
console.log(lower)// Returns "hello"
*Removes whitespace from both ends of a string.
let text = " hello ";
let trimmed = text.trim();
console.log(trimmed) // Returns "hello"
*Joins two or more strings and returns a new string.
let text1 = "Hello";
let text2 = "World";
let combined = text1.concat(" ", text2);
console.log(combined) // Returns "Hello World"
*Returns the index of the first occurrence of a specified substring. Returns -1 if not found.
let text = "HELLO WORLD";
let index = text.indexOf("O");
console.log(index)// Returns 4
*Replaces the first occurrence of a specified value with a new value.
let text = "HELLO WORLD";
let newText = text.replace("WORLD", "EVERYONE");
console.log(newText)// Returns "HELLO EVERYONE"
*Replaces all occurrences of a specified value with a new value.
let text = "HELLO WORLD WORLD";
let newText = text.replaceAll("WORLD", "EVERYONE");
console.log(newText)// Returns "HELLO EVERYONE EVERYONE"
*Splits a string into an array of substrings based on a specified separator.
let text = "HELLO WORLD";
let parts = text.split(" ");
console.log(parts)// Returns ["HELLO", "WORLD"]
*Joins the elements of an array into a string, using a specified separator.
let array = ["HELLO", "WORLD"];
let joined = array.join(" ");
console.log(joined)// Returns "HELLO WORLD"
*Checks if the string starts with the specified string.
let text = "HELLO WORLD";
let starts = text.startsWith("HELLO");
console.log(starts)// Returns true
*Checks if the string ends with the specified string.
let text = "HELLO WORLD";
let ends = text.endsWith("WORLD");
console.log(ends)// Returns true
*Checks if the string contains the specified substring.
let text = "HELLO WORLD";
let includes = text.includes("LO");
console.log(includes)// Returns true
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3