473,836 Members | 1,498 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

2 New Member
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
4 3000
TRScheel
638 Recognized Expert Contributor
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.Spl it(' ');

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

String.IsNullOr Empty(InputStri ng.Split(' ')[indexToBeChecke d]);

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
7,872 Recognized Expert Expert
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
cyberdrugs
2 New Member
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
638 Recognized Expert Contributor
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

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

Similar topics

2
5121
by: José Joye | last post by:
Hello, I was wondering if there is a method that exists to replace multi-spaces within a string with single-space. eg: "12 3 4 56" --> "12 3 4 56" I think this could be done by looking at each char within a loop and copying the char to a stringBuilder instance if current and previous char are not spaces...
5
22945
by: Christine | last post by:
I have a text file that appears to be delimited by multiple spaces. The split function will only work with one space if I am correct. Is there some way to split this file into an array without getting the extra spaces?
32
14932
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
5
29073
by: Greg Collins [InfoPath MVP] | last post by:
I couldn't find anything in my searches... I'm wondering if there's a Regex (with or without additional C# code) that can convert a either "lowerCamelCase" or "UpperCamelCase" into a proper "Title Case" (with spaces). Thanx! -- Greg Collins Please visit: http://www.InfoPathDev.com
9
2781
by: Graham | last post by:
I have been having some fun learning and using the new Controls and methods in .Net 2.0 which will make my life in the future easier and faster. Specifically the new databinding practises and wizards. But, I have found that trying to do something "outside the norm" adds a rather large level of complexity and/or data replication. Background I have been commissioned to create a web-based application for a client. It has a formsaunthentication...
5
5061
by: vonclausowitz | last post by:
Repost from an VB group. Hi All, I'm looking for a way to search for multiple words in a database. There is however one but. The words have to be within a certain range of each other. For example I would like to find records which contain the word MALE and within for example 10 words of it the word BROWN.
6
3993
by: Joseph Geretz | last post by:
I have the following class which I am serializing and passing back and forth between my Web Service application and the client. public class Token : SoapHeader { public string SID; public string UID; public string PWD; }
8
17516
by: Joe Cool | last post by:
I need to map several columns of data from one database to another where the data contains multiple spaces (once occurance of a variable number or spaces) that I need to replace with a single space. What would be the most efficient way to do this? I am using SQL2K. I was thinking a function since I know of no single Transact-SQL command that can accomplish this task.
10
10619
by: H | last post by:
Hi, I have the following address fields in a table: flat_number house_name_or_number street village postal_town county postcode
0
10846
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10595
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5828
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4458
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4021
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3116
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.