Home
Run >
<!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>