Tuesday, July 8, 2008

String Object in JavaScript

String Object in JavaScript

var str="Hello world!";

To get length of a string
var str_len = str.length;

To style strings
var str_big = str.big();
var str_small = str.small() ;
var str_bold = str.bold() ;
var str_italic = str.italics() ;
var str_blink = str.blink(); //(does not work in IE)
var str_fixed = str.fixed() ; //Displays a string as teletype text
var str_strike = str.strike() ;
var str_fc = str.fontcolor("Red") ;
var str_fs = str.fontsize(12) ;
var str_lc = str.toLowerCase() ;
var str_uc = str.toUpperCase() ;
var str_sub = str.sub() ;
var str_sup = str.sup() ;
var str_link = str.link("http://www.w3schools.com");

To get position of the first occurrence of a specified string value in a string
var str_pos = str.indexOf( "o" );

The lastIndexOf() method returns the position of the last occurrence of a specified string value, searching backwards from the specified position in a string.
var str_pos = str.lastIndexOf( "o" );

To use the match() method to search for a specified string value within a string and return the string value if found
var str_match = str.match( "world!" );

To use the replace() method to replace some characters with some other characters in a string.
var str_replace = str.replace( /world/, "JavaScript" );

The search() method is used to search a string for a specified value.
var str_search = str.search( /world/ );

The concat() method is used to join two or more strings.
var str_concat = str.concat("welcome", "to", "JavaScript");

The anchor() method is used to create an HTML anchor.
var str_anchor = str.anchor( "anchorName" );

The charAt() method returns the character at a specified position.
var str_charat = str.charAt( 1 ); //H

The charCodeAt() method returns the Unicode of the character at a specified position.
var str_charcode = str.charCodeAt( 1 ); //101

The fromCharCode() takes the specified Unicode values and returns a string.
var str_fromCharCode = String.fromCharCode( 72,69,76,76,79 ); //HELLO

The slice() method extracts a part of a string and returns the extracted part in a new string.
var str_slice = str.slice( 6 );

The split() method is used to split a string into an array of strings.
var str_split = str.split( " " );

The substr() method extracts a specified number of characters in a string, from a start index.
var str_substr = str.substr( 3 );

The substring() method extracts the characters in a string between two specified indices.
var str_substring = str.substring( 3 );

No comments: