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

Space delimiter

One of the things that drives me mad about vb.net is
spending time trying to make a program do something then
finding that there's already an inbuild procedure that
does exactly the same thing.

So before I begin on this can anyone tell me if there is
already a routine to do this:

I am dividing up a delimited textstring using
string.split. The delimiter character is a space, but
where there are adjacent spaces I want to treat these as a
single delimiter. What is the smart way to do this?

Grateful for any ideas
Nov 20 '05 #1
16 8215
yust a thought
strNew = strOld.replace(" "," ")
you could put it in a for next loop if you want to replace 3,4,... spaces 2

eric

"simonc" <an*******@discussions.microsoft.com> wrote in message
news:0d****************************@phx.gbl...
One of the things that drives me mad about vb.net is
spending time trying to make a program do something then
finding that there's already an inbuild procedure that
does exactly the same thing.

So before I begin on this can anyone tell me if there is
already a routine to do this:

I am dividing up a delimited textstring using
string.split. The delimiter character is a space, but
where there are adjacent spaces I want to treat these as a
single delimiter. What is the smart way to do this?

Grateful for any ideas

Nov 20 '05 #2
Cor
Hi Simonc

In addition to Eric did I make it with stringbuilder,
you asked something new and this is a new function who is real much faster
than all old string replace funtions. But I don't know if there is not a
better way.

Dim a As String = "There is a lot of for air here"
Dim sb As New System.Text.StringBuilder(a)
Do While sb.ToString.IndexOf(" ") > -1 '2 spaces
sb.Replace(" ", " ")
Loop
Dim b As String = sb.ToString
Dim c() As String = Split(b)
I hope this helps a little bit?

Cor
"simonc" <an*******@discussions.microsoft.com> schreef in bericht
news:0d****************************@phx.gbl...
One of the things that drives me mad about vb.net is
spending time trying to make a program do something then
finding that there's already an inbuild procedure that
does exactly the same thing.

So before I begin on this can anyone tell me if there is
already a routine to do this:

I am dividing up a delimited textstring using
string.split. The delimiter character is a space, but
where there are adjacent spaces I want to treat these as a
single delimiter. What is the smart way to do this?

Grateful for any ideas

Nov 20 '05 #3
On Wed, 5 Nov 2003 04:27:09 -0800, simonc wrote:

I am dividing up a delimited textstring using
string.split. The delimiter character is a space, but
where there are adjacent spaces I want to treat these as a
single delimiter. What is the smart way to do this?


Yet another method is to use regular expressions. This expression replaces
all occurrences of 2 or more spaces with a single space.

Imports System.Text.RegularExpressions

strNew = Regex.Replace(strOld, " {2,}", " ")

--
Chris

To send me an E-mail, remove the underscores and lunchmeat from my E-Mail
address.
Nov 20 '05 #4
i think we have a winner :)
ill have a look at those 2 ;)
Imports System.Text.RegularExpressions
strNew = Regex.Replace(strOld, " {2,}", " ")

Nov 20 '05 #5
Possibly more reliable way would be to use

Regex.Replace(yourStr,@"\s+",@"\s");

Please check definition of \s in regular expressions reference

HTH
Alex
"Chris Dunaway" <dunawayc@_lunchmeat_sbcglobal.net> wrote in message
news:hg*****************************@40tude.net...
On Wed, 5 Nov 2003 04:27:09 -0800, simonc wrote:

I am dividing up a delimited textstring using
string.split. The delimiter character is a space, but
where there are adjacent spaces I want to treat these as a
single delimiter. What is the smart way to do this?
Yet another method is to use regular expressions. This expression

replaces all occurrences of 2 or more spaces with a single space.

Imports System.Text.RegularExpressions

strNew = Regex.Replace(strOld, " {2,}", " ")

--
Chris

To send me an E-mail, remove the underscores and lunchmeat from my E-Mail
address.

Nov 20 '05 #6
Cor
Hi Eric,

Did you test it?
strNew = Regex.Replace(strOld, " {2,}", " ")


This is 50 to 100 times slower than that loop with the stringbuilder.

(I did test it)

So it depends on the size of the string what to use.

Cor

Nov 20 '05 #7
Simonc,
But wait there is more! ;-)

I would not bother with the Replace if I was using the Regular Expression.

I would simply do the Split based on the Regular Expression!
Imports System.Text.RegularExpressions
Dim strNew() As String ' an array of strings

Split based on one or more space characters:
strNew = Regex.Split(strOld, " +")

Alternatively you can split based on whitespace characters.
strNew = Regex.Split(strOld, "\s+")

"\s" which is a short cut for white space, includes more than just the space
character.

Hope this helps
Jay
"simonc" <an*******@discussions.microsoft.com> wrote in message
news:10****************************@phx.gbl... Many thanks. I like this one best of all.

Simon C
-----Original Message-----
On Wed, 5 Nov 2003 04:27:09 -0800, simonc wrote:

I am dividing up a delimited textstring using
string.split. The delimiter character is a space, but
where there are adjacent spaces I want to treat these as a single delimiter. What is the smart way to do this?


Yet another method is to use regular expressions. This

expression replaces
all occurrences of 2 or more spaces with a single space.

Imports System.Text.RegularExpressions

strNew = Regex.Replace(strOld, " {2,}", " ")

--
Chris

To send me an E-mail, remove the underscores and

lunchmeat from my E-Mail
address.
.

Nov 20 '05 #8
On Wed, 5 Nov 2003 17:51:13 +0100, Cor wrote:
Hi Eric,

Did you test it?
strNew = Regex.Replace(strOld, " {2,}", " ")


This is 50 to 100 times slower than that loop with the stringbuilder.

(I did test it)

So it depends on the size of the string what to use.

Cor


Thanks for pointing that out, but I am not seeing that. Here is the code I
used to time it. Here are the results:

First Run:

Method1: 156225 ticks
Method2: 0 ticks

Second Run (And every run after that):

Method1: 0 ticks
Method2: 0 ticks

Any idea why the first run on the regular expression would be slower, but
each subsequent run is not?

Thanks

'\\\\\\
Private Sub Button1_Click(...) Handles Button1.Click

Dim sNumber As String
Dim sResult As String
Dim ts As TimeSpan
Dim dStart As DateTime

sNumber = "This is a test string with lots of spaces"
dStart = Now
sResult = Method1(sNumber)
ts = Now.Subtract(dStart)
MsgBox(sResult & " Elapsed Time (1): " & ts.Ticks.ToString & " Ticks.")

dStart = Now
sResult = Method2(sNumber)
ts = Now.Subtract(dStart)

MsgBox(sResult & " Elapsed Time (2): " & ts.Ticks.ToString & " Ticks.")

End Sub

Private Function Method1(ByVal sInput As String) As String
Return RegularExpressions.Regex.Replace(sInput, " {2,}", " ")
End Function

Private Function Method2(ByVal sInput As String) As String
Dim sb As New System.Text.StringBuilder(sInput)
Do While sb.ToString.IndexOf(" ") > -1
sb.Replace(" ", " ")
Loop
Return sb.ToString
End Function

'//////
--
Chris

To send me an E-mail, remove the underscores and lunchmeat from my E-Mail
address.
Nov 20 '05 #9
On Wed, 5 Nov 2003 13:47:46 -0600, Jay B. Harlow [MVP - Outlook] wrote:
Simonc,
But wait there is more! ;-)

I would not bother with the Replace if I was using the Regular Expression.

I would simply do the Split based on the Regular Expression!


"We're not worthy!, We're not worthy!"

:)

I forgot about the split method in the RegEx!

--
Chris

To send me an E-mail, remove the underscores and lunchmeat from my E-Mail
address.
Nov 20 '05 #10
Chris,
I have an little "FAQ" (my OE Sent items currently) that I use to answer
questions from, here is the normal snippet I use when people ask about
Split.

---x--- cut here ---x---
There are three Split functions in VB.NET:

Use Microsoft.VisualBasic.Strings.Split if you need to split a string based
on a specific word (string). It is the Split function from VB6.

Use System.String.Split if you need to split a string based on a collection
of specific characters. Each individual character is its own delimiter.

Alternatively use System.Text.RegularExpressions.RegEx.Split to split based
on matching patterns.
---x--- cut here ---x---

Hope this helps
Jay

"Chris Dunaway" <dunawayc@_lunchmeat_sbcglobal.net> wrote in message
news:1v*****************************@40tude.net...
On Wed, 5 Nov 2003 13:47:46 -0600, Jay B. Harlow [MVP - Outlook] wrote:
Simonc,
But wait there is more! ;-)

I would not bother with the Replace if I was using the Regular Expression.
I would simply do the Split based on the Regular Expression!


"We're not worthy!, We're not worthy!"

:)

I forgot about the split method in the RegEx!

--
Chris

To send me an E-mail, remove the underscores and lunchmeat from my E-Mail
address.

Nov 20 '05 #11
Cor
Hi Chris
I did test your test.
This is a short string that I did y times
With that the
first regex : Stringbuilder : Jay regex split 1 : Jay regex split 2
9:3:9:18

I changed your code and did it loop I also did a test with a real long
string and then I get differences from 50 till 100 time. That code I did not
suply if you want it tell it.

This was just for fun, your somewhere changed code is below

Cor

\\\
Private Function Method1(ByVal sInput As String) As String()
Return Split(System.Text.RegularExpressions.Regex.Replace (sInput, " {2,}", "
"))
End Function
Private Function Method2(ByVal sInput As String) As String()
Dim sb As New System.Text.StringBuilder(sInput)
Do While sb.ToString.IndexOf(" ") > -1
sb.Replace(" ", " ")
Loop
Return Split(sb.ToString)
End Function
Private Function Method3(ByVal sInput As String) As String()
Return System.Text.RegularExpressions.Regex.Split(sInput, " +")
End Function
Private Function Method4(ByVal sInput As String) As String()
Return System.Text.RegularExpressions.Regex.Split(sInput, "\s+")
End Function
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim sNumber As String
Dim sResult As String()
Dim ts As Integer
Dim res As String
Dim m As Integer
Dim i As Double
Dim y As Double = 100000
sNumber = "This is a test string with lots of spaces"
ts = Environment.TickCount
For i = 0 To y
sResult = Method1(sNumber)
Next
ts = Environment.TickCount - ts
res = ""
For m = 0 To sResult.Length - 1
res = res & sResult(m).ToString & " "
Next
MsgBox(res & " Elapsed Time (1): " & ts.ToString & " Ticks.")
ts = Environment.TickCount
For i = 0 To y
sResult = Method2(sNumber)
Next
ts = Environment.TickCount - ts
res = ""
For m = 0 To sResult.Length - 1
res = res & sResult(m).ToString & " "
Next
MsgBox(res & " Elapsed Time (2): " & ts.ToString & " Ticks.")
ts = Environment.TickCount
For i = 0 To y
sResult = Method3(sNumber)
Next
ts = Environment.TickCount - ts
res = ""
For m = 0 To sResult.Length - 1
res = res & sResult(m).ToString & " "
Next
MsgBox(res & " Elapsed Time (3): " & ts.ToString & " Ticks.")
ts = Environment.TickCount
For i = 0 To y
sResult = Method4(sNumber)
Next
ts = Environment.TickCount - ts
res = ""
For m = 0 To sResult.Length - 1
res = res & sResult(m).ToString & " "
Next
MsgBox(res & " Elapsed Time (4): " & ts.ToString & " Ticks.")
End Sub
///
Nov 20 '05 #12
Cor
HI Jay B.

I thought your last metod would be it, but again stringbuilder wins.

I did only wanted to correct the testprogram from Chris so he could do the
test.

Look for it at Chris, I stop for tonight

If I did something wrong in the test, than it was an accident.

And tell it me?

Cor
Nov 20 '05 #13
On Wed, 5 Nov 2003 22:19:04 +0100, Cor wrote:
I did test your test.
This is a short string that I did y times
With that the
first regex : Stringbuilder : Jay regex split 1 : Jay regex split 2
9:3:9:18


That would seem to indicate that the stringbuilder is 3 times faster. I
got similar results using the code you posted (unchanged):

Method1 Method2 Method3 Method4
2141 750 2328 5141 ticks

Again the stringbuilder seems to be about 3 times faster, but not 50 to
100, unless I'm missing something.

It is an interesting experiement, to be sure.

Cheers!
--
Chris

To send me an E-mail, remove the underscores and lunchmeat from my E-Mail
address.
Nov 20 '05 #14
Cor,
I would not expect the Regex to be the fastest, I would however expect it to
be 'easier' to enter.

Which is where:

strNew = Regex.Split(strOld, " +")

Does seem very easy to enter ;-)

Thanks for the testing.
Jay

"Cor" <no*@non.com> wrote in message
news:%2******************@TK2MSFTNGP11.phx.gbl...
HI Jay B.

I thought your last metod would be it, but again stringbuilder wins.

I did only wanted to correct the testprogram from Chris so he could do the
test.

Look for it at Chris, I stop for tonight

If I did something wrong in the test, than it was an accident.

And tell it me?

Cor

Nov 20 '05 #15
Cor
Chris,

Now we where testing 100000 times a very short string in a loop.

In another test I did make a string from 1000000 long (I don't know it exact
anymore) and than you get those differences. The efficiency from the
stringbuilder is in building string, so there would be an upgoing line when
the strings become longer.

Cor
Nov 20 '05 #16
Hi Chris,

Any timing test that comes back with 0 is highly suspect!! ;-))

For a fair comparison you want some sort of loop that is going to take a
few seconds for each method.

For I = 1 To 100000 'add zeros as required

You might also want to experiment with different sizes of string

N = 100000
S = "h sl sd js da a sj kl sdj sd "
For Test = 1 To 5
DoTheTimingTests (N, S)
N = N / 2
S &= S
Next

Regards,
Fergus
Nov 20 '05 #17

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

Similar topics

6
by: Grumble | last post by:
Hello all, I want to read lines from a text file, where each line has the following syntax: token1:token2:token3 There could be white space between tokens and ':'
4
by: Raquel | last post by:
Could someone explain to me what the reason is for having a character delimiter (which is double quotes by default) for performing Loads/Imports on UDB? I would think that column delimiter along...
8
by: Dave | last post by:
Greetings, Is there a way I can split a string into an array on a space OR a carriage return? What would the code look like for this? Thanks, -Dave
4
by: Paul Hadfield | last post by:
Hi, Wonder if anyone can help me on this, In DotNet2.0 I've been quite happily using the WriteAttributeString method of XmlWriter object, but have run into a problem when trying to output a...
6
by: Charles | last post by:
This is very strange. In the following program, If I type a one-word name, it works fine. Now, if I type a two-word name, the program splits the variable into two: #include <iostream> #include...
1
by: cikail | last post by:
Hi there, I know there are a lot of this question in th ASP.Net section. But I'm currently in the dark. I've tried to find the answer from every search place but maybe my question was wrong. Heres...
1
by: Liz2 | last post by:
I am trying to read a flat file as the source file with space as the delimiter. I am using the "SQL Import and Export Wizard", and under the "demilited", there is the following options: {CR} {LF}...
4
by: kretik | last post by:
I've been trying to coax this class to use something other than the default '$' but it seems setting it to something else has no discernible effect. Is it necessary to inherit from the class to do...
7
by: ganesh gajre | last post by:
Hi all, I want to read file which is mapping file. Used in to map character from ttf to unicode. eg Map file contain data in the following way: 0 ० 1 १
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.