JavaScript provides a number of methods for manipulating strings. Here are some of the most commonly used methods:
-
charAt(): This method returns the character at a specified index in a string. -
concat(): This method concatenates two or more strings and returns a new string. -
indexOf(): This method returns the index of the first occurrence of a specified value in a string. -
slice(): This method extracts a section of a string and returns a new string. -
toUpperCase(): This method converts all characters in a string to uppercase. -
toLowerCase(): This method converts all characters in a string to lowercase.
Example:
rustconst str = "Hello, world!";
const char = str.charAt(1);
console.log(char); // Output: "e"
Example:
javascriptconst str1 = "Hello";
const str2 = "world";
const str3 = str1.concat(", ", str2, "!");
console.log(str3); // Output: "Hello, world!"
Example:
rustconst str = "Hello, world!";
const index = str.indexOf("world");
console.log(index); // Output: 7
Example:
rustconst str = "Hello, world!";
const newStr = str.slice(7, 12);
console.log(newStr); // Output: "world"
Example:
rustconst str = "Hello, world!";
const upperCaseStr = str.toUpperCase();
console.log(upperCaseStr); // Output: "HELLO, WORLD!"
Example:
rustconst str = "Hello, world!";
const lowerCaseStr = str.toLowerCase();
console.log(lowerCaseStr); // Output: "hello, world!"
These are just a few examples of the many string methods available in JavaScript. You can learn more about these methods and others in the JavaScript documentation.
0 Comments