String Manipulation Using replace() Method

This the series of JavaScript string manipulation. substring(), split(), charAt(), and now we play with replace() method.  replace() method used to replace specified sub string with other sub string. JavaScript replace() method is case sensitive.  For more detail I have two example, and the pattern of replace() method is:

stringObject.replace("pattern","replacement") 

pattern is specified sub string which will be replace (case sensitive).

replacement is other sub string which will replace the pattern.

1. The result of script below are same with original string object, because the string object doesn't contain sub string pattern.

   1: <script type="text/javascript">//<![CDATA[
   2:     var sObj ="String Manipulation Using split() Method";
   3:     alert( sObj.replace("Split","replace") );
   4: //]]></script>

The pattern is "Split" and string object doesn't contain this pattern, so the result is "String Manipulation Using split() Method".

2. The result of script bellow replace the original string object.

   1: <script type="text/javascript">//<![CDATA[
   2:     var sObj ="String Manipulation Using split() Method";
   3:     alert( sObj.replace("split","replace") );
   4: //]]></script>
The pattern is "split" and string object contain this pattern, so the result is "Manipulation Using replace() Method".

String Manipulation Using charAt() Method

After post about substring() method and split() method, now I still play with string manipulation, and today I use charAt() method. In JavaScript charAt() method used to retrieve character at the index of strings. The pattern is:

stringObject.charAt(Index)

Index is numeric value where the location of character which need to retrieved. Example:

   1: <script type="text/javascript">//<![CDATA[
   2:     var sObj ="String Manipulation";
   3:     alert( sObj.charAt(5) );
   4: //]]></script>

The result for script above is "g", without double quotes.

Manipulate String Object Using JavaScript split() Method

If we often used JavaScript language for web programing, sometimes we need to manipulate string object. There are many kind of string manipulation method on JavaScript. But Now I want to play with "split()" method.

JavaScript split() method split characters in a string with the specified pattern of character(s). "split()" syntax is

stringObject.split("pattern")

 

This is the example using split() method :

   1: <script type="text/javascript">//<![CDATA[
   2:     var sObj ="Data1,Data2,Data3";
   3:     var aObj = sObj.split(",");
   4:     for ( var i in aObj )
   5:         {
   6:             alert( aObj[i] );
   7:         } 
   8: //]]></script>

The example above will give the alert result "Data1", then "Data2", then "Data3" without double quotes.

The result of split() method is an array. So we can use the split method to convert string object to an array object.

Manipulate String Object Using JavaScript substring() Method

If we often used JavaScript language for web programing, sometimes we need to manipulate string object. There are many kind of string manipulation method on JavaScript. But Now I want to play with "substring()" method.

JavaScript substring() method extracts characters in a string between two specified index value. "substring()" syntax is

stringObject.substring(Start-Index,Stop-Index)

Start-Index value is numeric and this value is required for start the extraction index. Stop-Index value is numeric and it's optional. To extract characters from the end of the string object use negative Start-Index value. Start-Index starts at 0 and if the Stop-Index is omitted, substring() method will extracts from Start-Index to the end of the string object.

1. This is the example using substring() method without Stop-Index :

   1: <script type="text/javascript">//<![CDATA[
   2: var sObj ="Manipulate String";
   3: alert(sObj.substring(5));
   4:  //]]></script>

The example above will give the alert result "ulate String" , without double quotes.

2. This is the example using substring() method with Stop-Index :

   1: <script type="text/javascript">//<![CDATA[
   2: var sObj ="Manipulate String";
   3: alert(sObj.substring(5,13));
   4:  //]]></script>

And the result for script above is "ulate St", without double quotes.