JavaScript String Object

The string object is used to represent a sequence of character. The string value should be written in quote then only it will consider as string otherwise it will be consider as integer. This tutorial highlights the benefits of using the JavaScript string object.

Syntax of String Object

var val = new string(string)

String Properties

NameDescription
constructorIt is used to provide a reference to string function of the particular object.
lengthIt is used to return the number of characters present in string.
prototypeIt is used to add properties and methods.

Example of JavaScript String Property

<!DOCTYPE html>
<head>
<title>String Property Example</title>
</head>
<body>
<script type="text/javascript">
	var str = new String( "Welcome to JavaBeat" );
	document.write("Length of String <b>Welcome to JavaBeat</b>  is : " + str.length );
</script>
</body>
</html>
  • In the above example, we are using string property method.
  • We have used property called length which determines the number of characters present in string .
  • document.write(“String Length is:” + str.length ); line returns number of characters stored in a variable str.

Example Application Test

  • Save the file as string_property.html in your system.
  • Just open the file in the browser, you will see the below picture in the browser.

When the execution process is completed successfully we will get the following output:

JavaScript String Object

String Methods

NameDescription
substring( )This function is used to extract a string specified using the two index values that is start and end value.
toUpperCase( )It is used to convert the string value to upper case.
toLowerCase( )It is used to convert the string value to lower case.
slice( )It is used to return a part of the string as a new string.
split( )It is used to split string into array of substrings based on given delimiter.
substr( )It is used to return the characters in a string within the specified location.
indexOf( )Gives the first occurrence in the string.
lastIndexOf( )Gives the last occurrence in the string.
charAt( )It returns string at specified position.
charCodeAt( )It returns the string with Unicode at specified position.
search( )It is used to search first substring match in a regular expression.
match( )It is used to match the given string with regular expression
replace( )It is used to replace the string value with a other value.
concat( )Joins the two strings and represent it as one string .
toString( )It is used to convert anything into string.
valueOf( )It is used to return the value of created object of the given parameter.
toLocaleLowerCase( )It is used to convert the string value to lower case into specified locale.
toLocaleUpperCase( )It is used to convert the string value to upper case into specified locale

Example of String Methods

<!DOCTYPE html>
<head>
<title>String Methods Example</title>
</head>
<body>
	<script type="text/javascript">
		var a = "java";
		var b = "BEAT";

		document.write("String <b>java</b> in Uppercase is"+ a.toUpperCase() + "<br>");
		document.write("String <b>BEAT</b> in Lowercase is "+b.toLowerCase() +"<br>");

		var str1 = "Welcome to JavaBeat";
		document.write("Character at 5th position in string <b>Welcome to JavaBeat</b> is : " +str1.charAt(5)+"<br>");

		var str2 = "Splits-takes-one-argument-as-delimiter";
		document.write("Split this string <b>Splits-takes-one-argument-as-delimiter</b>:<br> ");
		var res1=str2.split("-");
		document.write(res1);
		</script>
</body>
</html>
 
  • In the above example ,we are using string methods.
  • We have used few methods in the program such as toUpperCase() , toLowerCase(), indexOf(), charAt() and split() methods.
  • The method toLowerCase() converts the string value to lower case and The method toUpperCase() converts the string value to upper case.
  • The method indexOf() method returns first occurrence of specified value in the string. Here we are looking occurrence of to in the string variable str=”Welcome to JavaBeat”.
  • The method charAt() determines character at specified position. Here we are looking for character at 5th position specified in the var str1 = “Welcome to JavaBeat” .
  • The method split() is used to split the string into array of substrings based on given delimiter as specified in the line var str2 = “Splits-takes-one-argument-as-delimiter”.

JavaScript String Object Demo

  • Save the file as string_methods.html in your system.
  • Just open the file in the browser, you will see the below picture in the browser.

When the execution process is completed successfully we will get the following output:

JavaScript String Object 1

String HTML Wrappers

NameDescription
big( )It is used to display the string into the bigger size.
small( )It is used to display the string in smaller size.
sup( )It is used to superscript the string.
sub( )It is used to subscript the string.
fontsize( )It is used to display the size of the font.
fontcolor( )It is used to display the string in a specific color.
strike( )It is used to strike out the string or text.
bold( )It is used to display the string in bold format.
italic( )It is used the display the string in italic format.
blink( )It is used to blink the string.
anchor( )It is used to link from one page to another page.
link( )It is used to give an URL of another resource.
fixed( )It is used to display string as teletype text. It returns the string embedded in tag.

Example of String Methods

<!DOCTYPE html>
<html>
<head>
<title>String HTML wrappers</title>
</head>
<body>
	<script type="text/javascript">

		var str = "JavaBeat";
		document.write("String variable: "+str);
		document.write("In italics "+ str.italics() + "<br>");

		document.write("In bold "+ str.bold() + "<br>");

		document.write("In small "+ str.small() + "<br>");

		document.write("In big "+ str.big() + "<br>");

		document.write("As link "+ str.link("http://www.javabeat.net") + "<br>");

		document.write("With font size as 6: "+ str.fontsize(6) + "<br>");
	</script>
</body>
</html>

 
  • In the above example, we are using string html wrappers.
  • We have used few methods in the program i.e. italics(),bold (),small() , big(), link() and fontsize().
  • document.write(str.italics() ) line displays the string in italic format.
  • document.write(str.bold() ) line displays the string in bold format.
  • document.write(str.small()) line displays the string in smaller size.
  • document.write(str.big())line displays the string in bigger size.
  • document.write(str.link(“http://www.javabeat.net”)) line is used to display URL of another resource.
  • document.write(c.fontsize( 6 )); line displays the string font size.

JavaScript String Object Methods Demo

  • Save the file as string_wrapper.html in your system.
  • Just open the file in the browser, you will see the below picture in the browser. Note that the browser must support HTML specification.

When the execution process is completed successfully we will get the following output:

JavaScript String Object 2

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*