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!
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.
Plater 7,872
Recognized Expert Expert
I was able to accomplish this two different ways: -
string initstring = "the quick brown fox jumped.";
-
string rn = initstring.Replace(" ", " ");
-
while (rn.IndexOf(" ") > -1)
-
{
-
rn = rn.Replace(" ", " ");
-
}
-
//rn now contains only single spaces
-
-
string[] bits = initstring.Split(" ".ToCharArray(),StringSplitOptions.RemoveEmptyEntries );
-
string sn = "";
-
foreach (string bp in bits)
-
{
-
if (sn != "")
-
sn += " ";
-
sn +=bp.Trim();
-
}
-
//sn now contains only single spaces
-
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)
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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?
|
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) ||
|
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
|
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...
| |
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.
|
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;
}
|
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...
|
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
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |