Javascript String Methods

String methods help you to play with strings data.


String Methods and Properties

Primitive values, like “Thomas Crane”, cannot have properties or methods (because they are not objects).

But in JavaScript, methods and properties are also allowed for primitive values, because JavaScript treats primitive values as objects while executing methods and properties.

Let’s have the list of JavaScript string methods with description.

MethodsDescription
charAt()It provides the char value present at the specified index.
charCodeAt()It provides the Unicode value of a character present at the specified index.
concat()It provides a combination of two or more strings.
indexOf()It provides the position of a char value present in the given string.
lastIndexOf()It provides the position of a char value present in the given string by searching a character from the last position.
search()It searches a specified regular expression in a given string and returns its position if a match occurs.
match()It searches a specified regular expression in a given string and returns that regular expression if a match occurs.
replace()It replaces a given string with the specified replacement.
substr()It is used to fetch the part of the given string on the basis of the specified starting position and length.
substring()It is used to fetch the part of the given string on the basis of the specified index.
slice()It is used to fetch the part of the given string. It allows us to assign positive as well negative index.
toLowerCase()It converts the given string into lowercase letter.
toLocaleLowerCase()It converts the given string into lowercase letter on the basis of host?s current locale.
toUpperCase()It converts the given string into uppercase letter.
toLocaleUpperCase()It converts the given string into uppercase letter on the basis of host?s current locale.
toString()It provides a string representing the particular object.
valueOf()It provides the primitive value of string object.
split()It splits a string into substring array, then returns that newly created array.
trim()It trims the white space from the left and right side of the string.

Here below is some generally used string methods are listed with examples:-


JavaScript String charAt(index) Method

         The JavaScript String charAt() method returns the character for the given index.

Input:-
   
<script>  
   var str="test string";  
   document.write(str.charAt(2));  
</script>

Output:-


JavaScript String concat(str) Method

         The JavaScript String concat(str) method concatenates or joins or aggregate two strings.

Input:-
<script>  
   var str1="test ";  
   var str2="concat example";  
   var str3=str1.concat(str2);  
   document.write(str3);  
</script>

Output:-


JavaScript String indexOf(str) Method

       The JavaScript String indexOf(str) method returns the index position for the given string.

Input:-
<script>  
   var str1="To get the index of string";  
   var n= str1.indexOf("the");  
   document.write(n);  
</script>

Output:-


JavaScript String lastIndexOf(str) Method

         The JavaScript String lastIndexOf(str) method returns the last index position for the given string.

Input:-
<script>  
   var str1="String to get the last index";  
   var n= str1.lastIndexOf("get");  
   document.write(n);  
</script>

Output:-


JavaScript String toLowerCase() Method

         The JavaScript String toLowerCase() method converts the given string in lowercase letters.

Input:-
<script>  
   var str1="CONVERT TO LOWERCASE";  
   var str2=str1.toLowerCase();  
   document.write(str2);  
</script>

Output:-


JavaScript String toUpperCase() Method

         The JavaScript String toUpperCase() method converts the given string in uppercase letters.

Input:-
<script>  
   var str1="convert to uppercase";  
   var str2=str1.toUpperCase();  
   document.write(str2);  
</script>

Output:-


JavaScript String slice(startIndex, endIndex) Method

         The JavaScript String slice(startIndex, endIndex) method returns the parts of   string from given startIndex to endIndex. In slice() method, startIndex is inclusive and endIndex is exclusive.

Input:-
<script>  
   var str1="abcdefgh";  
   var str2=str1.slice(3,6);  
   document.write(str2);  
</script>

Output:-


JavaScript String trim() Method

         The JavaScript String trim() method removes leading/starting and trailing/ending whitespaces from the string.

Input:-
<script>  
   var str1="     remove spaces    ";  
   var str2=str1.trim();  
   document.write(str2);  
</script>

Output:-


JavaScript String split() Method

Input:-
<script>  
   var str1="This is my string";  
   document.write(str1.split(" ")); //splits the given string.  
</script>

Output:-