Day 5 of 30 days of javascript
In this article we will learn about the string methods in javascript
charAt(index): Returns the character at the specified index in a string.
javascriptCopy codeconst str = 'Hello'; console.log(str.charAt(0)); // Output: "H"
toUpperCase(): Converts a string to uppercase.
javascriptCopy codeconst str = 'hello'; console.log(str.toUpperCase()); // Output: "HELLO"
toLowerCase(): Converts a string to lowercase.
javascriptCopy codeconst str = 'HELLO'; console.log(str.toLowerCase()); // Output: "hello"
indexOf(searchValue): Returns the index of the first occurrence of a specified value in a string.
javascriptCopy codeconst str = 'Hello world'; console.log(str.indexOf('o')); // Output: 4
slice(startIndex, endIndex): Extracts a section of a string and returns it as a new string.
javascriptCopy codeconst str = 'Hello world'; console.log(str.slice(6, 11)); // Output: "world"
substring(startIndex, endIndex): Returns the substring between two indices.
javascriptCopy codeconst str = 'Hello world'; console.log(str.substring(6, 11)); // Output: "world"
replace(searchValue, newValue): Searches a string for a specified value, and returns a new string where the specified value is replaced.
javascriptCopy codeconst str = 'Hello world'; console.log(str.replace('world', 'there')); // Output: "Hello there"
split(separator): Splits a string into an array of substrings based on a specified separator.
javascriptCopy codeconst str = 'apple,banana,orange'; console.log(str.split(',')); // Output: ["apple", "banana", "orange"]
trim(): Removes whitespace from both ends of a string.
javascriptCopy codeconst str = ' Hello world '; console.log(str.trim()); // Output: "Hello world"
startsWith(searchString): Checks whether a string starts with the specified characters.
javascriptCopy codeconst str = 'Hello world'; console.log(str.startsWith('Hello')); // Output: true
endsWith(searchString): Checks whether a string ends with the specified characters.
javascriptCopy codeconst str = 'Hello world'; console.log(str.endsWith('world')); // Output: true
includes(searchString): Checks whether a string contains the specified characters.
javascriptCopy codeconst str = 'Hello world'; console.log(str.includes('world')); // Output: true
concat(str1, str2, ...): Combines one or more strings into a single string.
javascriptCopy codeconst str1 = 'Hello'; const str2 = 'world'; console.log(str1.concat(' ', str2)); // Output: "Hello world"
charAt(index): Returns the character at the specified index in a string.
javascriptCopy codeconst str = 'Hello'; console.log(str.charAt(0)); // Output: "H"
charCodeAt(index): Returns the Unicode value of the character at the specified index in a string.
javascriptCopy codeconst str = 'Hello'; console.log(str.charCodeAt(0)); // Output: 72 (Unicode value for 'H')
repeat(count): Returns a new string with a specified number of copies of the original string.
javascriptCopy codeconst str = 'Hello'; console.log(str.repeat(3)); // Output: "HelloHelloHello"
match(regexp): Searches a string for a specified pattern and returns an array of matches.
javascriptCopy codeconst str = 'The rain in Spain falls mainly in the plain'; const matches = str.match(/ain/g); console.log(matches); // Output: ["ain", "ain", "ain", "ain"]
search(regexp): Searches a string for a specified pattern and returns the index of the first occurrence, or -1 if not found.
javascriptCopy codeconst str = 'The rain in Spain falls mainly in the plain'; console.log(str.search(/ain/g)); // Output: 5
padStart(targetLength, padString): Pads the current string with another string until the resulting string reaches the given length.
javascriptCopy codeconst str = '5'; console.log(str.padStart(3, '0')); // Output: "005"
padEnd(targetLength, padString): Pads the current string with another string until the resulting string reaches the given length.
javascriptCopy codeconst str = '5'; console.log(str.padEnd(3, '0')); // Output: "500"