Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 202
  • Last Modified:

really quick loop question

i have a string in the form something like:

"1 23 434  4 56 676 787 898
45 565 67 6 88776"

where the items are seperated by a tab, and there might be a new line or 2 in there as well

my question is
how can i loop through each element in the string. Not something like:
For i As Integer = 0 To text.Length - 1
                    If text.Substring(i, 1) <> Chr(9) Then
                     end if
next

as this will loop though every ELEMENT, but how can i loop through every ITEM instead, one-by-one

Thanks for the help
0
allff
Asked:
allff
  • 3
  • 3
  • 2
  • +1
1 Solution
 
Brian CroweDatabase AdministratorCommented:
look at the string.split methoc
0
 
allffAuthor Commented:
i did:
Dim splitString As String() = myString.Split(tabChar)

with the string
?myString
"10      10      10
10      10      10
"
and it gives me
?myString
{Length=5}
    (0): "10"
    (1): "11"
    (2): "12
13"
    (3): "14"
    (4): "15
"
How can i get it so the at the new line character is stored in a seperate index of the splitString, as i will need it there for later

i.e. how can i get it in the format:
?myString
{Length=5}
    (0): "10"
    (1): "11"
    (2): "12"
    (3): "<new line stored here>"
    (4): "13"
    (6): "14"
    (7): "15"

Thanks again for the help
0
 
Brian CroweDatabase AdministratorCommented:
the delimiter string can be a list of characters so you can include both the tab and newline as delimiters
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
allffAuthor Commented:
Not sure what you mean. Sorry mate

what doy uo mean by: you can include both the tab and newline as delimiters
Now would i implement what you are mentioning above?
0
 
Brian CroweDatabase AdministratorCommented:
dim delim as new char() {Chr(10), Chr(13), ...etc}

Dim splitString As String() = myString.Split(delim)
0
 
allffAuthor Commented:
i still get the same output when i split the string with  the delimiters i.e.

?splitString
{Length=7}
    (0): "10"
    (1): "11"
    (2): "12"
    (3): "
13"
    (4): "14"
    (5): "15"
    (6): "
"
using:
Dim delim As Char() = {Chr(9), Chr(13)}
delim(0) = Chr(9)
delim(1) = Chr(13)

Dim splitString As String() = text.Split(delim)

How can i get new line into a seperate index of splitString?

Sorry to drag this out for ages
0
 
rrbeckerCommented:
allff,

Try a Double pass, 'Split' on  newline chr(13) first  then 'Split' each remaining group....

rrbecker
0
 
rodmjayCommented:
Why dont you do a multiple split.

Dim str As String = "1 23 434  4 56 676 787 898" + vbCr + "45 565 67 6 88776" + vbCr + "223 234 235"

        Dim xx As String()
        Dim yy As String()
        Dim zz As New Collection

        xx = str.Split(vbCr)

        Dim i As Integer
        For i = 0 To xx.Length - 1

            Dim line As String = xx(i)
            yy = line.Split(vbTab)

            Dim ii As Integer
            For ii = 0 To yy.Length - 1

                zz.Add(yy(i))

            Next

            zz.Add("New Line")
        Next

Havent tested it but i hope you get the idea.  you can use array or collection, i prefer collection for the master.
0
 
rodmjayCommented:
excuse me, the second loop should use the ii instead of the i, sorry
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now