Google News
logo
Javascript String Methods
 javascript  string methods and examples.
charAt() :
JavaScript String charAt() method returns the character at the given index.
<html>
<head>
<title>charAt()</title>
</head>
<body>
 
<script type="text/javascript">

  var str = new String( "Java Script" );
  document.writeln("charAt(0) is:" + str.charAt(0)); 
  document.writeln("<br />charAt(1) is:" + str.charAt(1)); 
  document.writeln("<br />charAt(2) is:" + str.charAt(2)); 
  document.writeln("<br />charAt(3) is:" + str.charAt(3)); 

</script>
 
</body>
</html>
Output :

charAt(0) is:J

charAt(1) is:a

charAt(2) is:v

charAt(3) is:a

charCodeAt() :
A number indicating the Unicode value of the character.
<html>
<head>
<title>charCodeAt()</title>
</head>
<body>
 
<script type="text/javascript">

  var str = new String( "Java Script" );
  document.writeln("charAt(0) is:" + str.charCodeAt(0)); 
  document.writeln("<br />charAt(1) is:" + str.charCodeAt(1)); 
  document.writeln("<br />charAt(2) is:" + str.charCodeAt(2)); 
  document.writeln("<br />charAt(3) is:" + str.charCodeAt(3)); 

</script>
 
</body>
</html>
Output :

charAt(0) is:74

charAt(1) is:97

charAt(2) is:118

charAt(3) is:97

concat() :
JavaScript String concat() method concatenates or joins two strings.
<html>
<head>
<title>concat()</title>
</head>
<body>
 
<script type="text/javascript">

  var str1 = new String( "www.freetimelearnlearning.com , " );
  var str2 = new String( "www.freetimelearnlearn.com" );
  var str3 = str1.concat( str2 );
  document.write("String Concatenating : " + str3); 

</script>
 
</body>
</html>
Output :

String Concatenating : www.freetimelearnlearning.com , www.freetimelearnlearn.com

indexOf() :
JavaScript String indexOf(str) method returns the index position of the given string.
<html>
<head>
<title>String indexOf() Method</title>
</head>
<body>
 
<script type="text/javascript">

  var str1="My java script tutorial";  
  var number=str1.indexOf("script");  
  document.write(number);  

</script> 
 
</body>
</html>
Output :

8

lastindexOf() :
JavaScript String lastIndexOf() method returns the last index position of the given string.
<html>
<head>
<title>String lastindexOf() Method</title>
</head>
<body>
 
<script type="text/javascript">

  var str1="My java script tutorial";  
  var number=str1.lastIndexOf("java"); 
  document.write(number);  

</script>
 
</body>
</html>
Output :

3

match() :
Match a regular expression against a string.
<html>
<head>
<title>String match() Method</title>
</head>
<body>
 
<script type="text/javascript">

 var str_1 = "Javascript 1.4.7";
 var ma_1 = /(Javascript \d+(\.\d)*)/i;
 var ans = str_1.match( ma_1 );
 document.write(ans ); 

</script>
 
</body>
</html>
Output :

Javascript 1.4.7,Javascript 1.4.7,.7

replace() :
Replaces the regular expression (or sub string) portion (match) with the replaced text instead.
	
<html>
<head>
<title>String replace() Method</title>
</head>
<body>
 
<script type="text/javascript">
 
  var repl = /(\w+)\s(\w+)/;
  var str1 = "Reddy Ramana";
  var newstring = str1.replace(repl, "$2, $1");
  document.write(newstring);
 
</script>
 
</body>
</html>
Output :

Ramana, Reddy

search() :
Search for a match between a regular expression and a specified string.
<html>
<head>
<title>String search() Method</title>
</head>
<body>
 
<script type="text/javascript">

  var repl = /Free/gi;
  var str_1 = "Search your Website Free";
  if ( str_1.search(repl) == -1 ){
    document.write("Free Time Learning" );
  }
  else
  {
    document.write("Free Websites" );
  }

</script>
 
</body>
</html>
Output :

Free Websites

slice() :
Returns a substring of the string based on the “start” and “end” index arguments, NOT including the “end” index itself. “End” is optional, and if none is specified, the slice includes all characters from “start” to end of string.
<html>
<head>
<title>String slice() Method</title>
</head>
<body>
 
<script type="text/javascript">
  var str = "free time learning.";
  var sliced = str.slice(6, -4);
  document.write( sliced );
</script>
 
</body>
</html>
Output :

ime learn

split() :
Splits a string into many according to the specified delimiter, and returns an array containing each element. The optional “limit” is an integer that lets you specify the maximum number of elements to return.
<html>
<head>
<title>String split() Method</title>
</head>
<body>
 
<script type="text/javascript">

var str = "Free Time Learning my javascript tutorial";
var splitted = str.split(" ", 3);
document.write( splitted );

</script>
 
</body>
</html>
Output :

Free,Time,Learning

substr() :
Returns the characters in a string beginning at “start” and through the specified number of character.
<html>
<head>
<title>String substr() Method</title>
</head>
<body>
 
<script type="text/javascript">

var str = "free time learning, and learn javascript";
document.write("(1,3): "    + str.substr(1,3));
document.write("<br />(-2,5): "   + str.substr(-2,5));
document.write("<br />(1): "      + str.substr(1));
document.write("<br />(-15, 4): " + str.substr(-15,4));
document.write("<br />(20, 6): "  + str.substr(20,6));

</script>
 
</body>
</html>
Output :

(1,3): ree

(-2,5): pt

(1): ree time learning, and learn javascript

(-15, 4): earn

(20, 6): and le

substring() :
Returns the characters in a string between two indexes into the string.
<html>
<head>
<title>String substring() Method</title>
</head>
<body>
 
<script type="text/javascript">
  var str = "Free Time Learning, Learn JavaScript";
  document.write("(1,5): "    + str.substring(1,5));
  document.write("<br />(0,15): "   + str.substring(0, 15));
  document.write("<br />(10): "      + str.substring(10));
</script>
 
</body>
</html>
Output :

(1,5): ree

(0,15): Free Time Learn

(10): Learning, Learn JavaScript

toString() :
A string representing the specified object.
<html>
<head>
<title>String toString() Method</title>
</head>
<body>
 
<script type="text/javascript">
  var string = "Free Time Learning, Learn JavaScript";
  document.write(string.toString( ));
</script>
 
</body>
</html>
Output :

Free Time Learning, Learn JavaScript

toLowerCase() :
JavaScript String toLowerCase() method returns the given string in lowercase letters.
<html>
<head>
<title>String toLowerCase() Method</title>
</head>
<body>
 
<script type="text/javascript">
  var string = "Free Time Learnin, Learn JavaScript.";
  document.write(string.toLowerCase( ));
</script>
 
</body>
</html>
Output :

free time learnin, learn javascript.

toUpperCase() :
JavaScript String toUpperCase() method returns the given string in UPPERCASE letters.
<html>
<head>
<title>String toUpperCase() Method</title>
</head>
<body>
 
<script type="text/javascript">
  var str = "Free Time Learning, Learn Java Script.";
  document.write(str.toUpperCase( ));
</script>
 
</body>
</html>
Output :

FREE TIME LEARNING, LEARN JAVA SCRIPT.

trim() :
The JavaScript String trim() method removes leading and trailing whitespaces from the string.
<html>
<head>
<title>String trim() Method</title>
</head>
<body>
 
<script type="text/javascript">  
  var str1=" Learn JavaScript ";  
  var str2=str1.trim();  
  document.write(str2);  
</script>  
 
</body>
</html>
Output :

Learn JavaScript