varioyus functions

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am working with visual basic.net 1.1. I need to parse out data in a string
that is space delimited. The number of spaces between each data item in the
string can change. (I have no control over how many spaces are between each
item in the string since I am getting the data from a mainframe system over
the internet.)
I know that if I used the string function called split, I can split the
items in the string into items in an array. However, this is assuming the
delimiter is exactly the same. In this case, the number of spaces can change.
Thus I am looking for a solution to this parsing problem. I do not care if
I used the split string function. I would just like a solution that works.
Thanks!
 
Does your data itself have spaces in it?

You can still use split. Once you are done just go through your data and
look for elements that are single spaces and delete them.
 
Here is an example:

Dim str As String = "one two three four five "

Dim ar() As String

Dim st As String

ar = Split(str)

Debug.WriteLine(ar.Length)

For Each st In ar

If (st.Length > 0) Then

Debug.WriteLine(st)

End If

Next
 
Wendy said:
I am working with visual basic.net 1.1. I need to parse out data in a string
that is space delimited. The number of spaces between each data item in the
string can change. (I have no control over how many spaces are between each
item in the string since I am getting the data from a mainframe system over
the internet.)
I know that if I used the string function called split, I can split the
items in the string into items in an array. However, this is assuming the
delimiter is exactly the same. In this case, the number of spaces can change.
Thus I am looking for a solution to this parsing problem. I do not care if
I used the split string function. I would just like a solution that works.
Thanks!

You might want to use the regex split. Then you can specify, one or
more spaces...
 
Wendy,

Replace first all double spaces for a single, this you have to do twice
because when your string is from oneven lenght than it does not do the last
and leaves two spaces and than split it.

Very easy to do

\\\
str = str.replace(" "," ")
str = str.replace(" "," ")
dim strAr() as string = str.split(" "c)
///

I hope this helps,

Cor
 
Wendy,

Wendy Elizabeth said:
I am working with visual basic.net 1.1. I need to parse out data in a
string
that is space delimited. The number of spaces between each data item in
the
string can change. (I have no control over how many spaces are between
each
item in the string since I am getting the data from a mainframe system
over
the internet.)
I know that if I used the string function called split, I can split the
items in the string into items in an array. However, this is assuming the
delimiter is exactly the same. In this case, the number of spaces can
change.

\\\
Imports System.Text.RegularExpressions
..
..
..
Dim Data As String = "Bla Foo Goo Baz Boo"
Dim Parts() As String = Regex.Split(Data, "\s+")
For Each Part As String In Parts
MsgBox(Part)
Next Part
///
 
Tom Shelton:

What is the "regex split"? I have been working with visual basic.net for
about 3 months. That is why I am not familiar with "regex split".

Thanks!
 
Wendy Elizabeth said:
What is the "regex split"? I have been working with visual basic.net for
about 3 months. That is why I am not familiar with "regex split".

I suggest to read my reply, it includes a sample...
 
I suggest to read my reply, it includes a sample...
a *nice* sample

Still I am curious if my sample is not quicker, although the one you gave is
obvious nicer.

:-)

Cor
 
Cor Ligthert said:
a *nice* sample

Still I am curious if my sample is not quicker, although the one you gave
is obvious nicer.

Your approach will fail if parts are separated by more than four spaces. In
this case, multiple consecutive spaces will remain in the string and the
array of parts will contain empty elements.
 
Wendy,
In addition to the other comments.

..NET 2.0 (VS 2005) adds overloads to String.Split to allow removing
duplicate entries.

' VB 2005 syntax
Dim str As String = "one two three four five "
Dim values() As String = str.Split(New Char() {" "c},
StringSplitOptions.RemoveEmptyEntries)

In this instance:
- in VS 2005 I would use the above StringSplitOption
- in VS 2002 or VS 2003 I would use the RegEx approach...

Alternatively I have simply used String.Split or VB.Split and then simply
skipped the blank entries in the resulting array.

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


message |I am working with visual basic.net 1.1. I need to parse out data in a
string
| that is space delimited. The number of spaces between each data item in
the
| string can change. (I have no control over how many spaces are between
each
| item in the string since I am getting the data from a mainframe system
over
| the internet.)
| I know that if I used the string function called split, I can split the
| items in the string into items in an array. However, this is assuming the
| delimiter is exactly the same. In this case, the number of spaces can
change.
| Thus I am looking for a solution to this parsing problem. I do not care
if
| I used the split string function. I would just like a solution that
works.
| Thanks!
 
Herfried,
Your approach will fail if parts are separated by more than four spaces.
In this case, multiple consecutive spaces will remain in the string and
the array of parts will contain empty elements.
You did probably never try it.

This are methods be used in past to clean areas.

:-)

Cor
 
Cor,

Cor Ligthert said:
You did probably never try it.

This are methods be used in past to clean areas.

Yes, I actually tried the sample, and it does not work:

\\\
Dim Text As String = "Bla Foo Goo"
Text = Text.Replace(" ", " ")
Text = Text.Replace(" ", " ")
Dim Parts() As String = Text.Split(" "c)
For Each Part As String In Parts
MsgBox(Part)
Next Part
///

Resulting array: "Bla", "", "", "Foo", "", "Goo".
 
I think this would onyl work if you did it in a loop until the two spaces no
longer were found.
 
Herfried,

I have to admit, and I thought that I had used this last week.
The step is two so it will everytime do it once.

This will work, however than I am sure that your nice solution is as well
quicker.

\\\
Dim Text As String = "Bla Foo Goo"
For i As Integer = 0 To Text.Length \ 2
Text = Text.Replace(" ", " ")
Next
Text = Text.Replace(" ", " ")
Dim Parts() As String = Text.Split(" "c)
For Each Part As String In Parts
MsgBox(Part)
Next Part
///

:-)

Cor
 
Cor,
I would probably simply use a loop that looked for 2 spaces, if 2 spaces
were found I would do the replace.

Something like:

' VB 2005 syntax
Dim Text As String = "Bla Foo Goo"
Do While Text.Contains(" ")
Text = Text.Replace(" ", " ")
Loop
Dim Parts() As String = Text.Split(" "c)
For Each Part As String In Parts
MsgBox(Part)
Next Part

My concern with the above is the potentially excessive number of temporary
strings created.

NOTE: String.Contains is new to .NET 2.0.

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Herfried,
|
| I have to admit, and I thought that I had used this last week.
| The step is two so it will everytime do it once.
|
| This will work, however than I am sure that your nice solution is as well
| quicker.
|
| \\\
| Dim Text As String = "Bla Foo Goo"
| For i As Integer = 0 To Text.Length \ 2
| Text = Text.Replace(" ", " ")
| Next
| Text = Text.Replace(" ", " ")
| Dim Parts() As String = Text.Split(" "c)
| For Each Part As String In Parts
| MsgBox(Part)
| Next Part
| ///
|
| :-)
|
| Cor
|
|
 
Jay,

When I had sent my message to Herfried, I thought, something as I made now
bellow is probably the fastest (I did not test it on speed).

However, I thought, let me not go in discussion, telling that my first
approach was working was bad enough and the solution from Herfried is really
nice understandable code even that I hate regex. That because of regex it is
not really easy describing what is done.

(By the way I was in a way expecting that you would add something the same
as I made bellow)

:-)

\\\
Dim Text As String = "Bla Foo Goo"
Dim sb As New System.Text.StringBuilder(Text)
For i As Integer = sb.Length - 1 To 1 Step -1
If sb.Chars(i) = " "c Then
If sb.Chars(i - 1) = " "c Then
sb.Remove(i - 1, 1)
End If
End If
Next
Dim Parts() As String = sb.ToString.Split(" "c)
For Each Part As String In Parts
MsgBox(Part)
Next Part
///

Cor
 
Cor,
| Dim sb As New System.Text.StringBuilder(Text)
| For i As Integer = sb.Length - 1 To 1 Step -1
| If sb.Chars(i) = " "c Then
I really wish StringBuilder had an IndexOf & possibly a Contains on it, we
don't need them very often. But once in a while they would be oh so useful.
;-)

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Jay,
|
| When I had sent my message to Herfried, I thought, something as I made now
| bellow is probably the fastest (I did not test it on speed).
|
| However, I thought, let me not go in discussion, telling that my first
| approach was working was bad enough and the solution from Herfried is
really
| nice understandable code even that I hate regex. That because of regex it
is
| not really easy describing what is done.
|
| (By the way I was in a way expecting that you would add something the same
| as I made bellow)
|
| :-)
|
| \\\
| Dim Text As String = "Bla Foo Goo"
| Dim sb As New System.Text.StringBuilder(Text)
| For i As Integer = sb.Length - 1 To 1 Step -1
| If sb.Chars(i) = " "c Then
| If sb.Chars(i - 1) = " "c Then
| sb.Remove(i - 1, 1)
| End If
| End If
| Next
| Dim Parts() As String = sb.ToString.Split(" "c)
| For Each Part As String In Parts
| MsgBox(Part)
| Next Part
| ///
|
| Cor
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top