1

I need to extract(the easy part) a list of integers given through an array of strings gotten from: someString.split(separator) What I need is to put that resulting array(string) in a sortedSet How do I convert that? I've tried different things.

the current code is in VB

Dim _ports As New SortedSet(Of Integer) = Array.ConvertAll(portString.Split(","),Integer.Parse())

I've tried this but is not correct. I know that is simple to iterate though every single item and put it into the sortedSet but, is there any way to do it directly.

2
  • You need to use foreach loop to add items to sortedsets. It's the easiest way. Let me know If you need the loop. Commented Apr 26, 2014 at 7:17
  • i know that part, it's just iterating through every single item in the array and add it to the set: for each i in array..... What i want is to know any way to do it directly like shown above in the code Commented Apr 26, 2014 at 7:19

2 Answers 2

1

C# - Enumerable.Select will do conversion of string to integer if you pass int.Parse to it.

var resultingArray = new SortedSet<int>(portString.Split(',').Select(int.Parse));
Sign up to request clarification or add additional context in comments.

2 Comments

would i get an array or a sortedSet?
@Fernando - fixed - original was creating Array with ToArray, for SortedSet<int> just pass resulting enumerable to the constructor.
0

So, translating what Alexei Levenkov wrote to VB would be:

Dim resultingArray = New SortedSet(Of Integer)(portString.Split(","c).Select(AddressOf Integer.Parse))

Comments

Your Answer

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