Javascript string methods
JavaScript provides several built-in methods for working with strings. Here are some of the most commonly used string methods:
.length: returns the length of a string.
let str = "Hello World";
console.log(str.length); // 112 .charAt(index): returns the character at the specified index.
let str = "Hello World";
console.log(str.charAt(0)); // "H"
console.log(str.charAt(5)); // " "3. concat(string1, string2, ...): concatenates one or more strings and returns a new string.
let str1 = "Hello";
let str2 = "World";
let str3 = str1.concat(" ", str2);
console.log(str3); // "Hello World"4. indexOf(substring): returns the index of the first occurrence of a substring or -1 if the substring is not found.
let str = "Hello World";
console.log(str.indexOf("o")); // 4
console.log(str.indexOf("x")); // -15. lastIndexOf(substring): returns the index of the last occurrence of a substring or -1 if the substring is not found.
let str = "Hello World";
console.log(str.lastIndexOf("o")); // 7
console.log(str.lastIndexOf("x")); // -16. slice(start, end): returns a substring of a string, starting at the specified start index and ending before the specified end index.
let str = "Hello World";
console.log(str.slice(0, 5)); // "Hello"
console.log(str.slice(6)); // "World"7. substring(start, end): similar to .slice(), but does not accept negative indexes.
let str = "Hello World";
console.log(str.substring(0, 5)); // "Hello"
console.log(str.substring(6)); // "World"8. substr(start, length): returns a substring of a string, starting at the specified start index and having the specified length.
let str = "Hello World";
console.log(str.substr(0, 5)); // "Hello"
console.log(str.substr(6, 5)); // "World"9. replace(oldValue, newValue): replaces all occurrences of a specified value with a new value and returns a new string.
let str = "Hello World";
console.log(str.replace("Hello", "Goodbye")); // "Goodbye World"10. toUpperCase(): returns a new string with all characters in uppercase.
let str = "Hello World";
console.log(str.toUpperCase()); // "HELLO WORLD"11. toLowerCase(): returns a new string with all characters in lowercase.
let str = "Hello World";
console.log(str.toLowerCase()); // "hello world"12. trim(): returns a new string with leading and trailing white space removed.
let str = " Hello World ";Get Barath Kumar’s stories in your inbox
Join Medium for free to get updates from this writer.
console.log(str.trim()); // "Hello World"
13. split(separator): splits a string into an array of substrings based on a specified separator and returns the array.
let str = "Hello, World";
console.log(str.split(",")); // ["Hello", " World"]14. startsWith(substring): returns true if a string starts with the specified substring and false otherwise.
let str = "Hello World";
console.log(str.startsWith("Hello")); // true
console.log(str.startsWith("World")); // false15. endsWith(substring): returns true if a string ends with the specified substring and false otherwise.
let str = "Hello World";
console.log(str.endsWith("World")); // true
console.log(str.endsWith("Hello")); // false16. repeat(count): returns a new string with the original string repeated a specified number of times.
let str = "Hello";
console.log(str.repeat(3)); // "HelloHelloHello"17. match(regexp): returns an array of substrings that match a specified regular expression.
let str = "Hello World";
console.log(str.match(/l/g)); // ["l", "l"]18. search(regexp): returns the index of the first substring that matches a specified regular expression, or -1 if no match is found.
let str = "Hello World";
console.log(str.search(/l/)); // 219. split(separator, limit): returns an array of substrings by splitting a string into an array of substrings, using a specified separator. The optional limit parameter specifies the maximum number of splits.
let str = "Hello World";
console.log(str.split(" ")); // ["Hello", "World"]20. startsWith(searchString, position): returns a Boolean indicating whether the original string starts with the searchString.
let str = "Hello World";
console.log(str.startsWith("Hello")); // true21. endsWith(searchString, length): returns a Boolean indicating whether the original string ends with the searchString.
let str = "Hello World";
console.log(str.endsWith("World")); // true22. includes(searchString, position): returns a Boolean indicating whether the original string contains the searchString.
let str = "Hello World";
console.log(str.includes("World")); // true23. toString(): returns a string representation of the original string.
let str = "Hello World";
console.log(str.toString()); // "Hello World"24. normalize(): returns a new string with the Unicode Normalization Form of the original string.
let str = "résumé";
console.log(str.normalize()); // "résumé"Hope I have covered all the string functions. Missed any? add in comments section.






