Javascript String Object

In Javascript string object is used to hold a series of charecters and allows you to perform string manipulations, such as finding character index, replacing charecter, converting string in uppercase ot lowercase etc.

A Javascript can be any text inside double or single quotes and support variety of helper methods to manipulate on it.

Syntax

<html>
<head>
<title>My first Javascript code</title>
var stringOne = "Javascript suppert variety of helper methods";
var stringTwo = "Javascript is case sensitive language";
</head>
<body>
</body>
</html>

OR, you can use String global object directly

<html>
<head>
<title>My first Javascript code</title>
var obj = new String("Javascript suppert variety of helper methods");
</head>
<body>
</body>
</html>

Distinction between string primitives and String objects

JavaScript distinguishes between String objects and primitive string values. JavaScript automatically converts primitives value to String objects, so that it's allows you to use String object methods for primitive strings as well.

<html>
<head>
<title>My first Javascript code</title>
var string_prim = 'Some string';
var string_obj = new String(string_prim);
alert(typeof string_prim); // display "string"
alert(typeof string_obj);  // display "object"
</head>
<body>
</body>
</html>

In Javascript string primitives and string objects also give different results when you use eval(). Primitives passed to eval are treated as source code; String objects are treated as all other objects are, by returning the object. For example:

<html>
<head>
<title>My first Javascript code</title>
var str1 = '12 + 12';             // creates a string primitive
var str2 = new String('23 + 23'); // creates a String object
alert(eval(s1));        // returns the number 24
alert(eval(s2));        // returns the string "23 + 23"
</head>
<body>
</body>
</html>

String Properties

Sciens JavaScript treats primitive values as objects when executing methods and properties it supports all methods and properties are accessible to primitive type as well.

Here is a list of each property and their description.

PropertyDescription
constructor Returns a reference to the String function that created the object.
length Returns the length of the string.
prototype The prototype property allows you to add properties and methods to an object.

String Methods

Here is a list of string methods which is used to manipulate string object.

MethodDescription
charAt() Returns the character at the specified index.
charCodeAt() Returns a number indicating the Unicode value of the character at the given index.
concat() Combines the text of two strings and returns a new string.
indexOf() Returns the index within the calling String object of the first occurrence of the specified value, or -1 if not found.
lastIndexOf() Returns the index within the calling String object of the last occurrence of the specified value, or -1 if not found.
localeCompare() Returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order.
match() Used to match a regular expression against a string.
replace() Used to find a match between a regular expression and a string, and to replace the matched substring with a new substring.
search() Executes the search for a match between a regular expression and a specified string.
slice() Extracts a section of a string and returns a new string.
split() Splits a String object into an array of strings by separating the string into substrings.
substr() Returns the characters in a string beginning at the specified location through the specified number of characters.
substring() Returns the characters in a string between two indexes into the string.
toLocaleLowerCase() The characters within a string are converted to lower case while respecting the current locale.
toLocaleUpperCase() The characters within a string are converted to upper case while respecting the current locale.
toLowerCase() Returns the calling string value converted to lower case.
toString() Returns a string representing the specified object.
toUpperCase() Returns the calling string value converted to uppercase.
valueOf() Returns the primitive value of the specified object.
trim() Removes whitespace from both ends of a string.

String HTML Wrappers

Here is a list of each method which returns a copy of the string wrapped inside the appropriate HTML tag.

MethodDescription
anchor() Creates an HTML anchor that is used as a hypertext target.
big() Creates a string to be displayed in a big font as if it were in a <big> tag.
blink() Creates a string to blink as if it were in a <blink> tag.
bold() Creates a string to be displayed as bold as if it were in a <b> tag.
fixed() Causes a string to be displayed in fixed-pitch font as if it were in a <tt> tag
fontcolor() Causes a string to be displayed in the specified color as if it were in a <font color="color"> tag.
fontsize() Causes a string to be displayed in the specified font size as if it were in a <font size="size"> tag.
italics() Causes a string to be italic, as if it were in an <i> tag.
link() Creates an HTML hypertext link that requests another URL.
small() Causes a string to be displayed in a small font, as if it were in a <small> tag.
strike() Causes a string to be displayed as struck-out text, as if it were in a <strike> tag.
sub() Causes a string to be displayed as a subscript, as if it were in a <sub> tag
sup() Causes a string to be displayed as a superscript, as if it were in a <sup> tag

Javascript String Example

<!DOCTYPE html>
<HTML>  
<TITLE>Javascript String Constructor Example</TITLE>  
<HEAD>  
<script type="text/javascript">  
//constructor example code  
var str = new String( "This is string" );  
document.write("str.constructor is:" + str.constructor + "</br>");  
  
//length example code  
var str = new String( "This is string" );  
document.write("str.length is:" + str.length + "</br>");  
function book(title, author)
  {
    this.title = title;
    this.author = author;
  }
var myBook = new book("Javascript", "Jimi");  
book.prototype.price = null;  
myBook.price = 100;  
document.write("Book title is : " + myBook.title + "</br>");  
document.write("Book author is : " + myBook.author + "</br>");  
document.write("Book price is : " + myBook.price + "</br>");  
</script>  
</HEAD>  
<BODY>  
</BODY>  
</HTML>

Javascript String Example

<!DOCTYPE html>
<HTML>  
<HEAD>
  <!-- www.techstrikers.com -->
<TITLE>Javascript String Object Example</TITLE>  
<script type="text/javascript">   
var str = new String("This is Javascript string object");  
//charAt() example code  
document.write("str.charAt(0) is:" + str.charAt(0) + "</br>");  
document.write("str.charAt(1) is:" + str.charAt(1));  
document.write("</br>");  
//charCodeAt() example code  
document.write("str.charCodeAt(0) is:" + str.charCodeAt(0) + "</br>");  
document.write("str.charCodeAt(1) is:" + str.charCodeAt(1) + "</br>");  
document.write("</br>");  
//concat() example code  
var str1 = new String( "This is string one" );  
var str2 = new String( " This is string two" );  
var str3 = str1.concat( str2 );  
document.write("Concatenated String :" + str3 + "</br>");  
  
//indexOf() example code  
var index = str.indexOf( "string" );  
document.write("indexOf found String :" + index + "</br>" );  
  
//indexOf() example code  
var index1 = str.lastIndexOf( "string" );  
document.write("lastIndexOf found String :" + index1 + "</br>");  
  
//localeCompare() example code  
var index = str1.localeCompare( "AbCD ?" );  
document.write("localeCompare second :" + index + "</br>");  
  
//match() example code  
var re = /(chapter \d+(\.\d)*)/i;  
var found = str.match( re );  
document.write(found );  
  
//replace() example code  
var re = /apples/gi;  
var str = "Apples are round, and apples are juicy.";  
var newstr = str.replace(re, "oranges");  
document.write(newstr + "</br>");  
document.write("</br>");  
//search() example code  
var re = /apples/gi;  
var str = "Apples are round, and apples are juicy.";  
if ( str.search(re) == -1 ){  
            document.write("Does not contain Apples" + "</br>");  
}else{  
   document.write("Contains Apples" + "</br>");  
}  
  document.write("</br>");
//slice() example code  
var str = "Apples are round, and apples are juicy.";  
var sliced = str.slice(3, -2);  
document.write( sliced );  
  document.write("</br>");   
//split() example code  
var str = "Apples are round, and apples are juicy.";  
var splitted = str.split(" ", 3);  
document.write( splitted + "</br>");  
  document.write("</br>");
//substr() example code  
var str = "Apples are round, and apples are juicy.";  
document.write("(1,2): "    + str.substr(1,2));  
document.write("(-2,2): "   + str.substr(-2,2));  

  document.write("</br>");
  //substring() example code  
var str = "Apples are round, and apples are juicy.";  
document.write("(1,2): "    + str.substring(1,2) + "</br>");  
document.write("(0,10): "   + str.substring(0, 10) + "</br>");  

  document.write("</br>");
//toLocaleLowerCase() example code  
var str = "Apples are round, and Apples are Juicy.";  
document.write(str.toLocaleLowerCase( ) + "</br>");  
  
//toLocaleUpperCase() example code  
var str = "Apples are round, and Apples are Juicy.";  
document.write(str.toLocaleUpperCase( )+ "</br>");  
  
//toLowerCase() example code  
var str = "Apples are round, and Apples are Juicy.";  
document.write(str.toLowerCase( ) + "</br>");  
         
//toString() example code  
var str = "Apples are round, and Apples are Juicy.";  
document.write(str.toString( ) + "</br>");  
  
//toUpperCase() example code  
var str = "Apples are round, and Apples are Juicy.";  
document.write(str.toUpperCase( ) + "</br>");  
  
//valueOf() example code  
var str = new String("Hello world");  
document.write(str.valueOf( ) + "</br>");  
</script>  
</HEAD>  
<BODY>  
</BODY>  
</HTML>