IT Community - Software Programming, Web Development and Technical Support

Removing multiple spaces from string

This is a discussion on Removing multiple spaces from string within the C# Programming forums, part of the Software Development category; Hi, Could anyone tell me how to remove the multiple spaces in a string...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C# Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 03-04-2008, 06:26 AM
bluesky bluesky is offline
D-Web Analyst
 
Join Date: Jun 2007
Posts: 201
bluesky is on a distinguished road
Default Removing multiple spaces from string

Hi,
Could anyone tell me how to remove the multiple spaces in a string
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-04-2008, 06:28 AM
Sundaram Sundaram is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Location: chennai
Posts: 117
Sundaram is on a distinguished road
Send a message via MSN to Sundaram Send a message via Yahoo to Sundaram
Default Re: Removing multiple spaces from string

Hi bluesky,

Use the regular expression to remove multiple spaces in a string

// i have a variable amount of spaces between each word in this string
string test = "Womens and mens in a palace";
// replaces runs of multiple spaces with a single space
test = Regex.Replace(test, @"\s{2,}", " ");
// displays new string
MessageBox.Show(test);

I hope this would help for u
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-04-2008, 06:30 AM
Sundaram Sundaram is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Location: chennai
Posts: 117
Sundaram is on a distinguished road
Send a message via MSN to Sundaram Send a message via Yahoo to Sundaram
Default Re: Removing Double spaces from string

removing double-spaces from strings
// input string
string s = "blah blah";
// temporary for holding the string prior to replacing
string olds;

// repeatedly replace strings of 2 spaces with a single space
// until the result after replacement is the same as before,
// which implies that all spaces must be single spaces
do {
olds = s;
s = s.Replace(" ", " ");
} while (olds != s);
// now, s == "blah blah"

What bothers me about this solution is that it would be rather inefficient, particular for long strings, due to the way the .Net runtime deals with strings (given my current understanding anyway.) Some sort of StringBuilder or char array solution would probably be better I think.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-19-2008, 06:17 AM
GDevakii GDevakii is offline
D-Web Sr.Programmer
 
Join Date: Aug 2007
Posts: 138
GDevakii is on a distinguished road
Smile Re: Removing multiple spaces from string

public static string RemoveWhiteSpace(string line)
{
return Regex.Replace(line, "\\s+", " ");
}

private void button5_Click(object sender, EventArgs e)
{
string line = "This is ksdfj an example.";
Content.Text = Form1.RemoveWhiteSpace(line);
}
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
XML and white spaces Sathish Kumar XML and SOAP 2 03-14-2008 03:33 AM
Removing duplicate rows in a DataSet shaalini ASP and ASP.NET Programming 2 02-13-2008 09:33 PM
What is diffrenece between string.compare and string.compareordinal shaalini ASP and ASP.NET Programming 3 12-28-2007 10:46 PM
Trim Trailing and Leading Spaces using different databases srikumar_l Database Support 0 12-05-2007 03:17 AM
Info on removing pages history Jeyaseelansarc PHP Programming 2 09-25-2007 09:04 AM


All times are GMT -7. The time now is 05:10 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0