A string is a sequence of characters used to store text data.
let name = "Rohan";
let city = "Bangalore";Strings can be enclosed in "", '' or ``
For creating a string, new String("Hello") is rarely used, primitive and simple way is used to define and declare strings.
let message = "Hi";Characters of a string are accessed in the same way as array elements are accessed, i.e., is str = "Hello", str[0] is "H".
However String.at(index) function is also used... str.at(-1) will return the last character.
length property
let str = "javascript is amazing";
console.log(str.length); // returns the length of the string including blank spaces.- ToUppercase() - to convert all characters into uppercase alphabets
console.log("hi".toUpperCase()); // "HI"- toLowerCase() - to convert all characters into lowercase alphabets
console.log("HI".toLowerCase()); // "hi"- trim methods
console.log(" hi ".trim()); // "hi"
console.log(" hi ".trimStart()); // "hi "
console.log(" hi ".trimEnd()); // " hi"- includes() - returns true if the input string is a substring of the string
console.log("Javascript".includes("Java")); // true
console.log("Javascript".includes("Python")); // fasle- startsWitn and endsWith methods
console.log("Javascript".startsWith("Java")); // true
console.log("Javascript".startsWith("Python")); // fasle
console.log("Javascript".endsWith("script")); // true
console.log("Javascript".endsWith("lang")); // fasle- indexOf() and lastIndexOf() - return the first or last index of the substringin the string, or else reutrn -1 if not present
console.log("java".indexOf("a")); // 1
console.log("java".lastIndexof("a")); // 3- slice() - used to extract part of the string
console.log("Javascript".slice(4)); // from the 4th till last index --> "script"
console.log("Javascript".slice(0,4)); // from 0th to 4 -1 = 3rd index --> "Java"
console.log("Javascript".slice(-6)); // last 6 characters --> "script"- substring - similar to slice but has two non negative integer inputs
console.log("Javascript".substring(0,4)); // "Java"- replace and replaceAll - used to substitute parts of the string with replacement
console.log("ha ha ha".replace("ha","hi"); // "hi ha ha"
console.log("ha ha ha".replaceAll("ha","hi"); // "hi hi hi"- split(
delimiter) - used to convert string into and array
console.log("apples,mangoes,bananas".split(",")); // ["apples","mangoes","bananas"]- concat() - to attach a string in the end
console.log("Merry".concat(" and ","James"); // Merry and James- repeat() - used to repeat a string for a number of times
console.log("*".repeat(3)); // "***"- charAt() - returns the character at the index
console.log("Javascript".charAt(2)); // "v"- charCodeAt() - returns the ASCII value of the characte at the index
console.log("A".charCodeAt(0)); // 65- padStart() and padEnd() - pad the string to the spefic length by contcatinating the given character to the start or the end
console.log("5".padStart(3,"0")); // "005"
console.log("5".padEnd(3,"0")); // "500"- match() - to find substrings using regular expression
console.log("abc123".match(/\d+/)); // "123"- search() - finds the index of the substring
console.log("hello123".search("/\d/")); // 5previous