1

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?.

2
  • I'm confused as to what exactly you are trying to do, are you only interested in the integer values? Is this the only string that needs parsed? any more info would help Commented May 20, 2013 at 12:59
  • Please see my updated answer. You will like it. Commented May 20, 2013 at 13:30

4 Answers 4

2

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
Sign up to request clarification or add additional context in comments.

Comments

2

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

6 Comments

Ingenious but not too efficient. I think that using regular expressions is a good habit to get into.
@Bathsheba: so you think regex is super-efficient? :)
Probably not if Microsoft implemented the parser ;-)
@Bathsheba: Any universal processing is generally worse in performance than any specific implementation. If you provided performance analysis in your answer, and your regex is faster, we could talk about efficiency. :)
@Bathsheba: Maintainability and efficiency are two different things, often going in opposite directions of each other. So the more you get one, the more you lose the other.
|
0

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"

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.