These are some useful string methods in JavaScript
- String length
- String charAt()
- String concat()
- String indexOf()
- String lastIndexOf()
- String replace()
- String split()
- String substring()
- String toLowerCase()
- String toUpperCase()
- String slice()
- String trim()
String length
The length
property in string returns a length of string
Example:
// Example of String length property let str = "Recursive Minds" let length = str.length; console.log(length);
//Output 15
String charAt()
The charAt()
the method is used to find the index of a particular character let’s understand with an example
Example
// Example of charAt() method let str = "Recursive Minds" console.log(str.charAt(10));
// Output 'M'
String concat()
The concat()
method is used to concatenate strings
Example
//Example of concat() method let str1 = "Recursive"; let str2 = "Minds"; let output = ""; console.log(output.concat(str1," " ,str2)));
// Output "Recursive Minds"
String indexOf()
The indexOf()
method returns index of character lets understand using example
Example
// Example of indexOf() let str = "Recursive Minds" // It will print first occurence of 'e' in "Recursive Minds" console.log(str.indexOf('e'));
// Output 1
String lastIndexOf()
The lastIndexOf()
method returns the last index of occurence of given character lets understand with the example
Example
//Example of lastIndexOf() method let str = "Recursive Minds" // It will print last occurence of 'e' in "Recursive Minds" console.log(str.lastIndexOf('e'));
// Output 8
String replace()
The replace()
method returns a new String and replace given character from previous string let understand using example
Example
// Example of replace() method let str = "Sumit" // It will replace 'Su' with 'A' let ans = str.replace('Su', 'A'); console.log(ans);
// Output "Amit"
String split()
The split()
method used to split string into list of substring and returns them as array let’s understand using example
Example
// Example of split() method let str = "Recursive::Minds" // It will split "Recursive Minds" by '::' let ans = str.split('::'); console.log(ans)
// Output ["Recursive", "Minds"]
String substring()
The substring()
method return the part of string from start index to end index lets understand using example
// Example of substring() let str = "Recursive Minds" // It will return from substring from 0 to 8th index let ans = str.substring(0,9); console.log(ans);
//Output "Recursive"
String toLowerCase()
The toLowerCase()
method used to convert string into lowerCase lets understand using example
// Example of toLowerCase() method let str = "Recursive Minds" // It will convert string into lowercase and store into ans let ans = str.toLowerCase(); console.log(ans);
// Output "recursive minds"
String toUpperCase()
The toUpperCase()
method is used to convert string into uppercase lets understand using example
Example
// Example of toUpperCase() method let str = "Recursive Minds" // It will convert string into uppercase and store into ans let ans = str.toUpperCase(); console.log(ans);
// Output "RECURSIVE MINDS"
String slice()
The slice()
method returns section of string lets understand using the example
Example
// Example of slice() method let str = "Recursive Minds" let ans = str.slice(0, 9); console.log(ans);
// Output "Recursive"
String trim()
The trim()
the method is used to remove whitespaces from both ends of the string
Example
// Example of trim() method let str = " Recursive Minds " // It will remove all whitespaces from string let ans = str.trim(); console.log(ans);
// Output "Recursive Minds"
Thank you so much for reading
Happy Coding
For more details visit MDN Docs
Some other articles on JavaScript
- Introduction to JavaScript
- Variable in JavaScript
- JavaScript Data Types
- Let var const in JavaScript
- Operators in JavaScript
- JavaScript Conditionals
- for loop in JavaScript
- while and do-while in JavaScript
- break and continue in JavaScript
- functions in JavaScript
- Strings in JavaScript