473,387 Members | 1,760 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,387 software developers and data experts.

Trim "inbetween" whitespace

Hello,

I'd like to take a string like this "one two three four five"
and make it like this "one two three four five"

essentially replacing >1 spaces with 1. Can I do this somehow with the built-in functions?, I haven't found a way yet. I've tried using replace(" ", " "), but this has to be ran an unknown amount of times to trim down the string.

Thanks,
--Michael
Nov 20 '05 #1
5 1149

Gary,
Do While (MyString.IndexOf(" ") >= 0)
MyString = MyString.Replace(" ", " ")
Loop


That code would potentially create a lots of temporary strings, which
can hurt performance.
Michael,

I'd use Regexp or code my own function for it, something like this

Function InTrim(s As String, Optional remove As Char = " "c) As String
Dim buf(s.Length - 1) As Char
Dim n As Integer
Dim skip As Boolean

For Each c As Char In s
If Not skip OrElse c <> remove Then
skip = c = remove
buf(n) = c
n += 1
End If
Next
Return New String(buf, 0, n)
End Function

....

Console.WriteLine(InTrim("one two three four five"))

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 20 '05 #2
> Gary Miltonwrote:
Hi Michael,

Try this...

\\\
Dim MyString As String = "one two three four five"

Do While (MyString.IndexOf(" ") >= 0)
MyString = MyString.Replace(" ", " ")
Loop
///

HTH,
Gary


I believe the original question asked for any approach other than the
one you gave. I don't believe there is any way to solve this problem
more efficiently than an unknown number of replaces. (you could use
regular expressions, but my experience indicates that they would not
be nearly as efficient as the indicated loop).

I'd be curious to see if anyone has an alternative solution.

Nov 20 '05 #3

"Jimi" <ji************@yahoo-dot-ca.no-spam.invalid> wrote
I believe the original question asked for any approach other than the
one you gave. I don't believe there is any way to solve this problem
more efficiently than an unknown number of replaces. (you could use
regular expressions, but my experience indicates that they would not
be nearly as efficient as the indicated loop).

I'd be curious to see if anyone has an alternative solution.


If you are REALLY interested, and have a mind for math, check out
this post (and the surrounding thread it is in):

http://groups.google.com/groups?hl=e...phx.gbl#link28

(Be sure the whole link is used)

That very topic was discussed not long ago. To set up what he was going
after, you might want to scan through some of the posts from Bob O'Bob,
earlier in that thread. I had mine own results, but the guy above came in
with mathematics and pretty well ended the disucssion (with a calculated
answer).

<g>
LFS

Nov 20 '05 #4
Holy crap...

Nov 20 '05 #5

"Jimi" <ji************@yahoo-dot-ca.no-spam.invalid> wrote in message
news:40**********@127.0.0.1...
Gary Miltonwrote:

Hi Michael,

Try this...

\\\
Dim MyString As String = "one two three four five"

Do While (MyString.IndexOf(" ") >= 0)
MyString = MyString.Replace(" ", " ")
Loop
///

HTH,
Gary


I believe the original question asked for any approach other than the
one you gave. I don't believe there is any way to solve this problem
more efficiently than an unknown number of replaces. (you could use
regular expressions, but my experience indicates that they would not
be nearly as efficient as the indicated loop).

I'd be curious to see if anyone has an alternative solution.


Dim blnSpace as boolean
Dim i, j, intLen as integer
Dim MyString As String = "one two three four five"
Dim NewString as String

intLen = len(MyString)

for i = 0 to (intLen - 1)
if mid(MyString,i,1) = " " then
if blnSpace then
else
blnSpace = true
mid(NewString,j,1) = mid(MyString,i,1)
j += 1
else
blnSpace = false
mid(NewString,j,1) = mid(MyString,i,1)
j += 1
end if
next i

MyString = NewString

This would loop through the string only one time, byte by byte,
eliminating any subsequent spaces found after a space is found (then
resetting the boolean when any other character is found). I suspect you
could += each character when adding it to NewString, but I'm not positive
that using that method won't lose spaces as you add them to the end of the
string.

Regis

Nov 20 '05 #6

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

Similar topics

4
by: marco | last post by:
Hello, I'm putting together a php webpage which is parsing my (.html) bookmarks list. I want to give them a new lay-out with php and CSS. My question is: How can I make a function that counts...
10
by: Carlos Ribeiro | last post by:
Hello all. I'm sending this to the list because I would like to know if someone else has ever stumbled across this one, and also because one possible solution is to patch, or simply "decorate",...
2
by: Richard Morey | last post by:
Hi, I am trying to build an "in between" page to show an animated "Working.." gif while a long process runs on the server via ASP. The problem is that the table/gif is not displaying when I...
14
by: mshetty | last post by:
Hi, I get an error "Warning: b::a_method hides the virtual function a::a_method()." on compiling the following code.. #include <iostream.h> class a {
9
by: Don | last post by:
I understand that when I send a cookie in a client-side page containing JS, it isn't actually "set" until the next page is loaded. Is there some way to "set" it within the same html/JS page so it...
7
by: (Pete Cresswell) | last post by:
We were testing a version of our app that's been running for months with no problems and it started throwing "Object no longer exists" messages on two machines in the test environment. We tried...
38
by: axlq | last post by:
I'm trying to figure out how to display a box that has a width in "em" units. So far no luck. Below is some HTML that displays two rows of 30 'm' characters in lowercase and uppercase, followed...
4
by: GavReynolds | last post by:
Hi All! I am using Javascript to export some content in a popup (in this case, just 5 simple words) to a text file. This works fine and the user can save the text file. Upon opening the file,...
107
by: bonneylake | last post by:
Hey Everyone, i been working on trying to understand the URL method of retrieving information for the last week, but i am stuck. I been able to get one table of information, but now i need to get...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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...

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.