Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

jQuery Remove All whitespaces from string with Regular Expression

Oct 9, 2012
Introduction

Here I will explain how to remove or delete all white spaces from string using
JQuery in asp.net.

Description:
  
In previous articles I explained
jQuery Remove first/last character from string, jQuery Drag and Drop Example and many articles relating to JQuery. Now I will explain how to remove or delete white spaces from string using JQuery in asp.net.

To remove or delete white spaces from string we can use default jQuery function trim() but it will remove only starting and ending spaces but spaces within string will be same because of that we need to use regular expression to remove white spaces from string for that we need to write codesnippet like as shown below

Remove all the white spaces from string


<script type="text/javascript">
$(document).ready(function() {
var str = ' Welcome To Aspdotnet  -  Suresh '
var newstr = str.replace(/\s/g,'') // Remove white spaces from string
alert(newstr);
})
</script>
If you want check this code in sample check below code

Example:


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>jQuery Remove White spaces from string in asp.net</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
String : <b>'   Welcome To Aspdotnet  -  Suresh   '</b>
<input type="button" id="btnRemove" value="Remove White Spaces From String" /><br /><br />
</div>
<script type="text/javascript">
$(document).ready(function() {
$('#btnRemove').click(function() {
var str = ' Welcome To Aspdotnet  -  Suresh '
var newstr = str.replace(/\s/g, '') // Remove white spaces from string
alert(newstr);
});
});
</script>
</form>
</body>
</html>
Live Demo

To check Live demo click on below button

String : ' Welcome To Aspdotnet - Suresh '


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

0 comments :

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.