473,763 Members | 9,161 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8255
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*******@disc ussions.microso ft.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.Str ingBuilder(a)
Do While sb.ToString.Ind exOf(" ") > -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*******@disc ussions.microso ft.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.Reg ularExpressions

strNew = Regex.Replace(s trOld, " {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.Reg ularExpressions
strNew = Regex.Replace(s trOld, " {2,}", " ")

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

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

Please check definition of \s in regular expressions reference

HTH
Alex
"Chris Dunaway" <dunawayc@_lunc hmeat_sbcglobal .net> wrote in message
news:hg******** *************** ******@40tude.n et...
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.Reg ularExpressions

strNew = Regex.Replace(s trOld, " {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(s trOld, " {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.Reg ularExpressions
Dim strNew() As String ' an array of strings

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

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

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

Hope this helps
Jay
"simonc" <an*******@disc ussions.microso ft.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.Reg ularExpressions

strNew = Regex.Replace(s trOld, " {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(s trOld, " {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(dS tart)
MsgBox(sResult & " Elapsed Time (1): " & ts.Ticks.ToStri ng & " Ticks.")

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

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

End Sub

Private Function Method1(ByVal sInput As String) As String
Return RegularExpressi ons.Regex.Repla ce(sInput, " {2,}", " ")
End Function

Private Function Method2(ByVal sInput As String) As String
Dim sb As New System.Text.Str ingBuilder(sInp ut)
Do While sb.ToString.Ind exOf(" ") > -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

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

Similar topics

6
16175
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
11516
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 should have sufficed (of course it is the responsibility of the person performing the Load to ensure that the character he has chosen as column delimiter does not exist as a part of data). The problem I am facing is that I have a file generated...
8
14198
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
3254
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 space character as the value of an attribute. Prototype of the method I am using is XmlWriter.WriteAttributeString (String, String) in the following fashion:
6
2280
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 <string> int main() { std::cout << "What is your name? "; std::string name;
1
1267
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 the problem.. delimiter = " " 'can be either whitespace or others StringA = "Here I'm currently finding/solutions//but couldnt find one.." arr = Split(StringA, delimiter)
1
5449
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} {;} {:} {t} {,} {|} When I tried to use {CR} (carriage return}, it read in the whole line of course. Is there a way I can read in correctly a flat file with spaces as the delimiters? Thanks,
4
3427
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 this? I've only been using Python for a couple of weeks so I'm not sure what the best approach is here. Thanks in advance.
7
23308
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 १
0
9564
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9387
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10002
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9938
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9823
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8822
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7368
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.