By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,983 Members | 1,627 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,983 IT Pros & Developers. It's quick & easy.

C# - Convert Multiple Spaces into Single Spaces within a string

P: 2
Hi guys 'n gals,

I have a string which contains multiple spaces, and I would like to convert the multiple spaces into single spaces.

Example Input:
the quick brown fox jumps over the lazy dog

Example Output:
the quick brown fox jumps over the lazy dog

Anybody able to help me out please?


Cheers!
Jun 11 '07 #1
Share this Question
Share on Google+
4 Replies


TRScheel
Expert 100+
P: 638
Hi guys 'n gals,

I have a string which contains multiple spaces, and I would like to convert the multiple spaces into single spaces.

Example Input:
the quick brown fox jumps over the lazy dog

Example Output:
the quick brown fox jumps over the lazy dog

Anybody able to help me out please?


Cheers!
Ah, when I went to quote you, I realized that you actually intended there to be noticable differences between the two strings. Reading it on the post was decieving...

Have you tried

InputString.Split(' ');

And then looked at what it returns? I believe you will get a lot of strings that return true when passed through:

String.IsNullOrEmpty(InputString.Split(' ')[indexToBeChecked]);

And if so, you can just pull out the strings that actually have something in them (the words) and then put it back together with a space in between each one.
Jun 11 '07 #2

Plater
Expert 5K+
P: 7,872
I was able to accomplish this two different ways:
Expand|Select|Wrap|Line Numbers
  1. string initstring = "the  quick brown   fox          jumped.";
  2. string rn = initstring.Replace("  ", " ");
  3. while (rn.IndexOf("  ") > -1)
  4. {
  5.     rn = rn.Replace("  ", " ");
  6. }
  7. //rn now contains only single spaces
  8.  
  9. string[] bits = initstring.Split(" ".ToCharArray(),StringSplitOptions.RemoveEmptyEntries );
  10. string sn = "";
  11. foreach (string bp in bits)
  12. {
  13.     if (sn != "")
  14.        sn += " ";
  15.     sn +=bp.Trim();
  16. }
  17. //sn now contains only single spaces
  18.  
Jun 11 '07 #3

P: 2
Hi TRScheel,

I'd prefer not to have to break the string apart to get what I am after if possible...


Hi Plater,

Just tried for you first solution, and it does the charm, and it's quite fast as well (at least for what I am doing)


Thanks for the replies! :o)
Jun 11 '07 #4

TRScheel
Expert 100+
P: 638
Hi TRScheel,

I'd prefer not to have to break the string apart to get what I am after if possible...


Hi Plater,

Just tried for you first solution, and it does the charm, and it's quite fast as well (at least for what I am doing)


Thanks for the replies! :o)

Ya, after looking at Plater's solution, it is much more elegant.
Jun 12 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.