Remove extra spaces from string using regular expression, asp.net, c#

Removes extra space from the string using Regular expression.

public static readonly RegexOptions Options =          RegexOptions.IgnorePatternWhitespace |
RegexOptions.Singleline;
public string RemoveExtaSpaces(string text)
{
Regex regex = new Regex(@”\s{2,}”, Options);
text = regex.Replace(text.Trim(), ” “); //This line removes extra spaces and make space exactly one.
//To remove the  space between the end of a word and a punctuation mark used in the text we will
//be using following line of code

regex=new Regex(@”\s(\!|\.|\?|\;|\,|\:)”); // “\s” whill check for space near all puntuation marks in side ( \!|\.|\?|\;|\,|\:)”); )
text = regex.Replace(text, “$1″);
return text;

}

Above function removes all extra spaces in the string to make one space exactly. Feel free to use, its been used and tested

static void Main(string[] args)

{

string mytext = “blah    blah    blah   blah   .”;

Console.WriteLine(mytext);

Console.WriteLine(RemoveExtaSpaces(mytext));

}

Result

blah blah blah blah.

I Wan to share this post:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • kick.ie
  • Live
  • MyShare
  • IndianPad
  • Reddit
  • StumbleUpon
  • Technorati
  • YahooMyWeb
  • DotNetKicks
  • DZone
  • TwitThis

JavaScript ShowModalDialog + Aspx page_load event not fires

I was using following code to show  Popup window

//opens a pop up window
function PopupWindow(url) {
mywindow =window.open( url, “Popupwindow”,
“location=1,status=0,scrollbars=1,width=800,
height=600,resizable=1,toolbar=0″ )

Above code was showing Popwindow then Requirement changed and we needed a Modal popup Window.  Then I found followin code which provides required functionality.

//opens a pop up window
function PopupWindow(url) {
var ie=document.all;
if (ie)
window.showModalDialog(url,”Window”,”status:no; help:no;
dialogWidth:1200px; dialogHeight:768px”);
else
{
var mywindow =window.open( url, “Popupwindow”,
“location=1,status=no,scrollbars=1,width=1200px,
height=768px,resizable=1,toolbar=0″ )
mywindow.focus();
}


I was using above code to show Audit and History of data for a specified field. This was not working 100% as When we open page in ModalWindow using ShowModalDialog it Caches all data. So the Page_Load Event of my .aspx page was not firing after 1st click for that specific field . As Page_Load event was not firing, I was unable to get the latest changes made by the user.  To See the latest changes made by user we were closing the browser and doing the whole process again i.e. open browser , login , search that record and see the history which was a pain.

To stop the page caching I am now using the page directiv

<%@ OutputCache Location=”None” %>

Above directive is not allowing page to cache anything so my problem is solved.

I can show the latest changes made by the user now. :)

I Wan to share this post:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • kick.ie
  • Live
  • MyShare
  • IndianPad
  • Reddit
  • StumbleUpon
  • Technorati
  • YahooMyWeb
  • DotNetKicks
  • DZone
  • TwitThis