Web Development

Some Simple String Objects in JavaScript

Some simple string objects in JavaScript are given below:-

fromCharCode() : It takes char code and convert corresponding string.

var myString;
myString= YouString.fromCharCode(65,66,67);

// another example
s="";
var CC;
for (CC=65; CC <=90; CC++)
{
 s=s+String.fromCharCode(CC);
}
// output will : ABCDEFGHIJKLMNOPQRSTUVWXYZ

indexOf() and lastIndexOf() take two parameters:

  • The string you want to find
  • The character position you want to start searching from (optional)
var myString="I am palash";
var position;
position=myaString.indexOf("palash");
// It returns the position of substring in the main string


position=myString.lastIndexOf("palash");
position=myString.indexOf("wrox", position);


The substr() and substring() Methods:

If we wanted to cut out part of a string and assign that cut out part to another variable or use it in an expression, we would use the substr() and substring() methods. Both methods provide the same end result, that is a part of a string, but they differ in the parameters they require.

The method substring() takes two parameters:

  • The character start position and the character end position of the part of the string we want.
  • The second parameter is optional If you don’t include it, all characters from the start position to the end of the string are included.
// substring()
 var myString="JavaScript";
 var mySubString=myString.substring(0,4);

The method substr() again takes two parameters:

  • The first being the start position of the first character you want included in your substring.
  • The second parameter specifies the length of the string of characters that you want to cut out of the longer string.
// substring()
 var myString="JavaScript";
 var mySubString=myString.substr(0,4);

The toLowerCase() and toUpperCase() Methods- Changing the Case of a String:-

// toLowerCase() and toUpperCase() Methods
 var myString="I don't Care About Case";
 if(myString.toLowerCase()=="i don't care about case")
 {
     // write something...
 }

That’s enough today !!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s