Split Whitespace?

  • Thread starter Thread starter Raterus
  • Start date Start date
R

Raterus

Hello,

I'm trying to come up with an algorithm that will parse a search string, entered into a textbox. Basically it is not going to be too complicated, it will replace any delimeters the user puts in (comma's, semicolons, etc..) with spaces, essentially ignoring them, and then split the resultant string into a string array.

I've posted earlier about removing this extra whitespace that would be generated from the replace operation, so I can then split on " ", but now I'm wondering if there isn't an easier way to go about this. Removing this whitespace is really only needed to prep the string for the split function.

For example, say someone enters this

"bob, mary jane, sue; harry"

I want my final array to be this
bob
mary
jane
sue

NOT

bob
""
mary
jane
""
sue
""
harry

It would be like this if I didn't remove the extra spaces in the original string. This is the only way I really know how to do it right now, replace/remove extra whitespace

Does anyone have a better way of doing this?
Thanks!
--Michael
 
* "Raterus said:
I'm trying to come up with an algorithm that will parse a search string, entered into a textbox. Basically it is not going to be too complicated, it will replace any delimeters the user puts in (comma's, semicolons, etc..) with spaces, essentially ignoring them, and then split the resultant string into a string array.

I've posted earlier about removing this extra whitespace that would be generated from the replace operation, so I can then split on " ", but now I'm wondering if there isn't an easier way to go about this. Removing this whitespace is really only needed to prep the string for the split function.

For example, say someone enters this

"bob, mary jane, sue; harry"

I want my final array to be this
bob
mary
jane
sue

NOT

bob
""
mary
jane
""
sue
""
harry

It would be like this if I didn't remove the extra spaces in the
original string. This is the only way I really know how to do it right
now, replace/remove extra whitespace

\\\
MsgBox(System.Text.RegularExpressions.Regex.Replace("Foo bar Goo", " ( )*", " "))
///
 
Hi Raturus,

Copied from something else however I think this works fine (and fast)

Dim delimStr As String = " ,.:;#"
Dim delimiter As Char() = delimStr.ToCharArray()
Dim Test As String = "Hello person did"
Dim ResultAr() As String = Test.Split(delimiter)

And if you have sometimes two white spaces you can place a replace for that
split.

Cor
 
Hi Raterus,

I agreee with Alex on this one - take a look at Regex - you ill find a lot
of very useful techniques that you can apply to many things. It is very easy
then to do the split that you ar aiming to achived based on the comma
delimiter. There are a number of very good sites on Regex if you look at
Google. Start with a couple of simple Regexes.

I used something like below to convert an address delimited by commas into
an address block. Knowing my coding, this may not be the most elegant way -
but ot works for me!

Imports System.Text.RegularExpressions


Dim SplitAddress() As String
Dim strAddressElement As String
Dim mAddress As String

SplitAddress = Regex.Split(mAddress, ",")

For Each strAddressElement In SplitAddress
mDemographics += strAddressElement.Trim & ControlChars.CrLf
Next


Best wishes

Paul Bromley
 
Addendum:

Untested:

\\\
Imports System.Text.RegularExpressions
..
..
..
Dim astr() As String = Regex.Split("Foo bar Goo", " +")
Dim s As String
For Each s In astr
Debug.WriteLine(s)
Next s
///
 
* "Paul Bromley said:
I agreee with Alex on this one - take a look at Regex - you ill find a lot
of very useful techniques that you can apply to many things. It is very easy
then to do the split that you ar aiming to achived based on the comma
delimiter. There are a number of very good sites on Regex if you look at
Google. Start with a couple of simple Regexes.

Full ACK. A good ressource about regular expressions can be found here
(in addition to the .NET 'Regex' and Regular Expressions documentation):

<URL:http://www.regular-expressions.info/>
 
You might want to use regular expressions (Regex class) to parse such
strings and related Groups/Matches collections in it.
HTH
Alex

Hello,

I'm trying to come up with an algorithm that will parse a search string,
entered into a textbox. Basically it is not going to be too complicated, it
will replace any delimeters the user puts in (comma's, semicolons, etc..)
with spaces, essentially ignoring them, and then split the resultant string
into a string array.

I've posted earlier about removing this extra whitespace that would be
generated from the replace operation, so I can then split on " ", but now
I'm wondering if there isn't an easier way to go about this. Removing this
whitespace is really only needed to prep the string for the split function.

For example, say someone enters this

"bob, mary jane, sue; harry"

I want my final array to be this
bob
mary
jane
sue

NOT

bob
""
mary
jane
""
sue
""
harry

It would be like this if I didn't remove the extra spaces in the original
string. This is the only way I really know how to do it right now,
replace/remove extra whitespace

Does anyone have a better way of doing this?
Thanks!
--Michael
 
Back
Top