473,569 Members | 2,870 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 2986
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
5062
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
22931
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
14783
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
29039
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
2761
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...
5
5025
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
3973
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
17496
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...
10
10580
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
7693
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7962
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...
0
6277
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5501
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...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
933
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...

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.