JavaScript String Methods

Here are some commonly used string methods in javascript:


1. `length`: returns the length of a string.

javascript
const message = 'hello, world!';
console.log(message.length); // output: 13

2. `tolowercase()`, `touppercase()`: converts the string to lowercase or uppercase.

javascript
const message = 'hello, world!';
console.log(message.tolowercase()); // output: 'hello, world!'
console.log(message.touppercase()); // output: 'hello, world!'

3. `trim()`: removes leading and trailing whitespace from a string.

javascript
const message = ' hello, world! ';
Console.log(message.trim()); // output: 'hello, world!'

4. `split(separator)`: splits a string into an array of substrings based on a specified separator.

javascript
const message = 'hello, world!';
console.log(message.split(' ')); // output: ['hello,', 'world!']

5. `substring(startindex, endindex)`: extracts a portion of a string between the specified indices.

javascript
const message = 'hello, world!';
console.log(message.substring(0, 5)); // output: 'hello'
console.log(message.substring(7)); // output: 'world!'

6. `indexof(searchvalue)`, `lastindexof(searchvalue)`: finds the index of the first or last occurrence of a substring within the string.

javascript
const message = 'hello, world!';
console.log(message.indexof('o')); // output: 4
console.log(message.lastindexof('o')); // output: 8

7. `replace(searchvalue, replacement)`: replaces occurrences of a substring with another substring.

javascript
const message = 'hello, world!';
console.log(message.replace('world', 'universe')); // output: 'hello, universe!'

8. `charat(index)`, `charcodeat(index)`: returns the character or unicode value at a specified index.

javascript
const message = 'hello, world!';
console.log(message.charat(4)); // output: 'o'
console.log(message.charcodeat(4)); // output: 111

9. `startswith(searchvalue)`, `endswith(searchvalue)`: checks if a string starts or ends with a specified substring.

javascript
const message = 'hello, world!';
console.log(message.startswith('hello')); // output: true
console.log(message.endswith('world')); // output: false

10. `includes(searchvalue)`: checks if a string contains a specified substring.

javascript
const message = 'hello, world!';
console.log(message.includes('world')); // output: true

11. `slice(startindex, endindex)`: extracts a section of a string between the specified indices.

javascript
const message = 'hello, world!';
console.log(message.slice(7, 12)); // output: 'world'

12. `concat(str1, str2, ..., strn)`: concatenates multiple strings.

javascript
const str1 = 'hello, ';
const str2 = 'world!';
console.log(str1.concat(str2)); // output: 'hello, world!'

These are just a few examples of the many string methods available in javascript. They allow you to manipulate, search, extract, and transform strings according to your needs.



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext