473,398 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Stripping spaces and newline characters

Smart algorithm for normalizing strings anyone?

Example:
If the original string looks like this:
"the brown
fox "

I want it end up like this:
"the brown fox"
Thanx in advance
Nov 15 '05 #1
5 3194
Thor,

I think the smartest (and perhaps the most efficient) would be to use a
regular expression and then perform a replace operation. However, I don't
know the regular expression you would use. So, in lieu of that, I would use
the Split method on the string class and then insert the spaces myself in
between the individual elements.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Thor W Hammer" <thorwhammer - NO @ SPAM - hotmail.com> wrote in message
news:3f*********************@read.news.no.uu.net.. .
Smart algorithm for normalizing strings anyone?

Example:
If the original string looks like this:
"the brown
fox "

I want it end up like this:
"the brown fox"
Thanx in advance

Nov 15 '05 #2
Hi Thor,

str.Trim().Replace("\n", " " );

will do the trick
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Thor W Hammer" <thorwhammer - NO @ SPAM - hotmail.com> wrote in message
news:3f*********************@read.news.no.uu.net.. .
Smart algorithm for normalizing strings anyone?

Example:
If the original string looks like this:
"the brown
fox "

I want it end up like this:
"the brown fox"
Thanx in advance

Nov 15 '05 #3
I don't think it will change multiple white-spaces between words to single
white-spaces.

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:OL*************@TK2MSFTNGP11.phx.gbl...
Hi Thor,

str.Trim().Replace("\n", " " );

will do the trick
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Thor W Hammer" <thorwhammer - NO @ SPAM - hotmail.com> wrote in message
news:3f*********************@read.news.no.uu.net.. .
Smart algorithm for normalizing strings anyone?

Example:
If the original string looks like this:
"the brown
fox "

I want it end up like this:
"the brown fox"
Thanx in advance


Nov 15 '05 #4
However since my string was a well-formed xml document I did it like this
instead:

public string Normalize(string s)
{
XmlDocument d=new XmlDocument();
d.LoadXml(s);
return d.OuterXml;
}

"Thor W Hammer" <thorwhammer - NO @ SPAM - hotmail.com> wrote in message
news:3f*********************@read.news.no.uu.net.. .
I don't think it will change multiple white-spaces between words to single
white-spaces.

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote in message news:OL*************@TK2MSFTNGP11.phx.gbl...
Hi Thor,

str.Trim().Replace("\n", " " );

will do the trick
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Thor W Hammer" <thorwhammer - NO @ SPAM - hotmail.com> wrote in message
news:3f*********************@read.news.no.uu.net.. .
Smart algorithm for normalizing strings anyone?

Example:
If the original string looks like this:
"the brown
fox "

I want it end up like this:
"the brown fox"
Thanx in advance



Nov 15 '05 #5
Hi Thor,

Yes, you are right about that :)

Sorry, I did not saw it on your example, but if you get really complex then
you will have to take into account things like a tab ( \b ) so the
"normalization" can become quite complex ;)

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Thor W Hammer" <thorwhammer - NO @ SPAM - hotmail.com> wrote in message
news:3f*********************@read.news.no.uu.net.. .
I don't think it will change multiple white-spaces between words to single
white-spaces.

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote in message news:OL*************@TK2MSFTNGP11.phx.gbl...
Hi Thor,

str.Trim().Replace("\n", " " );

will do the trick
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Thor W Hammer" <thorwhammer - NO @ SPAM - hotmail.com> wrote in message
news:3f*********************@read.news.no.uu.net.. .
Smart algorithm for normalizing strings anyone?

Example:
If the original string looks like this:
"the brown
fox "

I want it end up like this:
"the brown fox"
Thanx in advance



Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Tim Mavers | last post by:
I am using the MailMessage class and am dynamically building the message body field. I know I am using String and not String builder but I don't want to worry about that now. The problem is after...
2
by: David Pratt | last post by:
I am working with a text format that advises to strip any ascii control characters (0 - 30) as part of parsing data and also the ascii pipe character (124) from the data. I think many of these...
3
by: Andy B | last post by:
I've tried using Trim or RTrim to strip trailing space characters from my data. When I check on the transformed data space characters are still there. We have an address table containing two...
16
by: John Smith | last post by:
Hi all Why doesn't this seem to work? void stripnl(char *sz) { char *nl; nl = strchr(sz, '\n'); if (nl) {
11
by: gopal srinivasan | last post by:
Hi, I have a text like this - "This is a message containing tabs and white spaces" Now this text contains tabs and white spaces. I want remove the tabs and white...
7
by: Raj | last post by:
Hi I was hoping someone could suggest a simple way of stripping non-numeric data from a string of numbers. For example, if I have "ADB12458789\n" I would like to remove the letters and the...
6
by: eight02645999 | last post by:
hi wish to ask a qns on strip i wish to strip all spaces in front of a line (in text file) f = open("textfile","rU") while (1): line = f.readline().strip() if line == '': break print line
2
by: Craig Buchanan | last post by:
I have a HTML fragment that looks like this: <tr> <td valign="top" nowrap><span class="textBold">Property ID: </span></td> <td valign="top" nowrap colspan="4"...
3
by: stathisgotsis | last post by:
Hello everyone, Trusting K&R2 i thought until recently that spaces are ignored in scanf's format string. Reading arguments to the contrary confused me a little. So i now ask: Is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...
0
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,...
0
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...

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.