Sathish
Sathish

Reputation: 4487

Using two array in single for each loop

In my application i have string like this 1,2,3&&4,5,6. Now i want check each and every element in single for each loop. Is it possible or not? If its possible how can i acheive this?.

Am trying using split method. But if i am using split method i want more than loop.

Like

dim sa as string=1,2,3&&4,5,6

for each x as string in sa.split("&&")
  for each y as string in x.split(",")
    ''' Here My Process
  next
next

How can over come this?. how can change to single loop?. It is possible or not?.

Upvotes: 1

Views: 585

Answers (4)

Victor Zakharov
Victor Zakharov

Reputation: 26424

String.Split has an overload that accepts an array of string delimiters:

Dim input As String = "1,2,3&&4,5,6"
For Each element As String In input.Split({",", "&&"}, StringSplitOptions.None)
  'do your processing on (1,2,3,4,5,6)
Next

Upvotes: 2

Alistair Findlay
Alistair Findlay

Reputation: 1154

One option would be to split on the "," and the "&" via the Split method and ignore empty entries, as such:

Dim sa As String = "1,2,3&&4,5,6"
Dim split As String() = sa.Split(New Char() {",", "&"}, StringSplitOptions.RemoveEmptyEntries)

For Each value In split
    Debug.WriteLine(value)
Next

Upvotes: 0

Epsil0neR
Epsil0neR

Reputation: 1704

As I understand, you want to use only one for each instead of using for each in for each.

You can first split by "&&" and then join with ",":

dim sa as string=1,2,3&&4,5,6
dim stringArray = String.Join(",", sa.split("&&")).split(",")

for each x as string in stringArray
end for

Upvotes: 2

Bathsheba
Bathsheba

Reputation: 234695

You could split using a regular expression as a delimiter:

Imports System.Text.RegularExpressions 'goes at the top of the module

For Each x As String In Regex.Split(sa, "\,|&&")

Where the regular expression means "comma or two ampersands". Note that you need to 'escape' the comma using a backslash; this is because commas do something special in regular expressions.

Don't forget to enclose your string in quotes:

dim sa as string="1,2,3&&4,5,6"

Upvotes: 0

Related Questions