Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Javascript Javascriptexamples JavaScript Remove Spaces

Related Tutorials


 
 

Share on Google+Share on Google+

JavaScript Remove Spaces

Advertisement
This page discusses - JavaScript Remove Spaces

JavaScript Remove Spaces

        

In this section, yow will learn how to remove spaces from the string.

To remove the spaces, we have simply used JavaScript methods split() and join(). Now to use these methods, we have defined a string. Firstly the split(" ") method splits the string into an array of strings. It takes the string argument, therefore the method enclosed a pair of double quotes which allow the string to be split. Then the method join("") join all the elements of an array into a string. As this method contains a parameter 'separator' which adds all array elements by placing the separator between them. In our example, to make the original string spaces less, first we split the original string by spaces using split(' ') and then join all elements with no space string using join('').

Here is the code:

<html>
<h2>JavaScript Remove Spaces</h2>
<script language="javascript" type="text/javascript">
var string="All glitters are not gold";
var newString=string.split(' ').join('');
document.write("The original string is = "+string+"<br>");
document.write("After removing the spaces, the string becomes = "+newString);
</script>
</html>

Output will be displayed as:

Download Source Code:

        

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 18, 2011

Related Tutorials

Discuss: JavaScript Remove Spaces  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com