-
length: Returns the length of the string.
Example:
var str = "Hello";
console.log(str.length); // Output: 5
-
charAt(index): Returns the character at the specified index in the string.
Example:
var str = "Hello";
console.log(str.charAt(0)); // Output: "H"
-
concat(string1, string2, ...): Concatenates strings onto the end of the string.
Example:
var str = "Hello";
var newStr = str.concat(" World", "!");
console.log(newStr); // Output: "Hello World!"
-
indexOf(searchValue, startIndex): Returns the first index of the search value from the specified index in the string.
Example:
var str = "Hello World!";
console.log(str.indexOf("o")); // Output: 4
-
lastIndexOf(searchValue, startIndex): Returns the last index of the search value from the specified index in the string.
Example:
var str = "Hello World!";
console.log(str.lastIndexOf("o")); // Output: 7
-
slice(startIndex, endIndex): Slices a portion of the string and returns the sliced part.
Example:
var str = "Hello World!";
var newStr = str.slice(6, 11);
console.log(newStr); // Output: "World"
-
substring(startIndex, endIndex): Slices a portion of the string and returns the sliced part. Unlike slice, startIndex cannot be greater than endIndex.
Example:
var str = "Hello World!";
var newStr = str.substring(6, 11);
console.log(newStr); // Output: "World"
-
substr(startIndex, length): Slices a portion of the string from the specified index to a specific number of characters and returns the sliced part.
Example:
var str = "Hello World!";
var newStr = str.substr(6, 5);
console.log(newStr); // Output: "World"
-
replace(searchValue, replaceValue): Replaces the first occurrence of the search value in the string with the replace value.
Example:
var str = "Hello World!";
var newStr = str.replace("World", "Universe");
console.log(newStr); // Output: "Hello Universe!"
-
toUpperCase(): Converts all characters in the string to uppercase.
Example:
var str = "Hello World!";
console.log(str.toUpperCase()); // Output: "HELLO WORLD!"
-
toLowerCase(): Converts all characters in the string to lowercase.
Example:
var str = "Hello World!";
console.log(str.toLowerCase()); // Output: "hello world!"
-
trim(): Removes whitespace from the beginning and end of the string.
Example:
var str = " Hello World! ";
console.log(str.trim()); // Output: "Hello World!"
These are the explanations of all the methods in the String object in JavaScript.