5

i have a comma separated string of numbers inside of a var named num_str

the contents of num_str looks like this: "1,2,3,4,5" etc

i am looking for a way to add num_str to an expression to convert the sting of numbers contained therein to an array of integers

i want to make sure i can simply reference 'num_str' to get the numbers from instead of spelling it out like {"1,2,3,4,5"}

i have tried this, where 'num_str' contains the numbers

Dim test As String = Nothing
Dim result() As Integer = Int32.TryParse(num_str.Split(","c))
For i = 0 To result.Length - 1
   test += result(i)
Next

but that doesn't work

what i am looking for is a result with an array of numbers

Andrea
  • 11,801
  • 17
  • 65
  • 72
user2835653
  • 192
  • 1
  • 3
  • 15
  • This code does the conversion wrong (you cannot convert a whole array "in one go"; but its elements. Bear in mind hat the one-line huMpty duMpty answer hides actually a loop through all the array elements). After this "small issue", it intends to add all the numbers to a variable called test (which is also wrongly declared because it should be an integer). From your comment in my (deleted) answer, you don't seem to be looking for that. I recommend you to explain exactly what you want (not feeling like helping you further anyway). – varocarbas Oct 01 '13 at 21:01
  • in num_str i have a string of numbers. when i search for MAX it gives me a text match (highest number with a '9' in front of it). so i need to convert those numbers to integers, store them in an array, and then find the MAX value. that what i am trying to do. Thanks again. – user2835653 Oct 01 '13 at 21:07
  • This is not what your code does, this is not what is written in your question and thus nobody will understand it. I recommend you to update your question to reflect what you want: helping you on a so simple request can be straightforward or extremely difficult; it is up to you. I personally will not help you; if you want to take my recommendation do it, otherwise not. – varocarbas Oct 01 '13 at 21:11

4 Answers4

15
Dim totalValue = str.
                 Split(","c).
                 Select(Function(n) Integer.Parse(n)).
                 Sum()
jcwrequests
  • 1,132
  • 1
  • 7
  • 13
4

Try this to create integer array

Dim numbers = str.Split(","c).[Select](Function(n) Integer.Parse(n)).ToList()

Then you can use the loop you are using at the moment to append value to string

huMpty duMpty
  • 14,346
  • 14
  • 60
  • 99
  • The OP seems to want to add all the elements into one variable (test), not storing them into a different array (the storage is a temporary step to get what he wants). In any case, I am not sure about the advantages of your code with respect to the one I proposed. – varocarbas Oct 01 '13 at 16:56
  • 1
    @varocarbas: Ok, I have updated the answer, One line thats it!! – huMpty duMpty Oct 01 '13 at 17:02
  • I saw that. This is a relevant second answer :) I personally don't like LINQ too much, but do think that quite a few people prefer it. (Perhaps you should set something to avoid errors?) – varocarbas Oct 01 '13 at 17:04
  • ?! Should I say everyone? I don't prefer it. In complex situations, perhaps. By default and always, not: it tends to make things seem differently than what they are; your line is basically doing the same than my code (I mean... if my code would be converting instead of adding), but without non-numeric checking; it is not faster than mine either. – varocarbas Oct 01 '13 at 17:05
  • @varocarbas: Seems you have confused with `linq` and `lambda expression`. Please have a look [Difference between LINQ Queries & Lambda expression](http://stackoverflow.com/questions/5327897/difference-between-linq-queries-lambda-expression). And also, it is much faster!! – huMpty duMpty Oct 01 '13 at 17:21
  • Well... I mean all the querying-based code ;) That is, converting a loop into a line, although it still is a loop. (Unexperienced) people reading your code, might think that it is faster, or that the whole array is converted in one go (one line = one assignation), this is what I meant. I use the generic LINQ reference to anything on these lines. Sorry for being so imprecise, I will intend to refer to these issues properly in the future. – varocarbas Oct 01 '13 at 17:26
  • PS: I don't have any problem with lambda expressions (using functions as variables), although use them only when required. What I have "problems" (prefer to avoid unless strictly required) with are query-based statements, LINQ or similar; no matter if they rely on lambda expressions or not. For iterating through various elements I prefer loops; unless the number of elements, categories and relationships between elements and categories is too complex and a query-based solution sounds more recommendable (that is, the situation becomes DB-like, what a query is for). – varocarbas Oct 01 '13 at 17:31
  • This is not lambda, this is LINQ – varocarbas Oct 01 '13 at 17:35
  • Lambda is Function(n) -> function as variable, taken as argument by a property ("Select") which takes functions as arguments (= you call them lambda expressions), which is a query-based statement. – varocarbas Oct 01 '13 at 17:37
  • That's why I call LINQ to everything: Select as property of an array or Select as an individual LINQ statement, does not make any true difference (not to me; both do the same). Using one option or the other is what my loop is "against" and that's why I call them with the common name LINQ, because "a buen entendedor, pocas palabras bastan" (not sure about the English translation) and what matters (at least to me) is the result: something working; its exact name is unimportant. – varocarbas Oct 01 '13 at 17:39
  • Actually, your statement lambda vs. LINQ is not correct. As explained, the reason why you call lambda to some query-based situations is because take lambda expressions (functions as variables) as arguments. I do accept this differentiation and don't care/complain at all (because of also thinking that are two different things). But some days ago, Reed Copsey proved me wrong in something I wouldn't ever expect to be wrong on: I have used array.Contains thousands of times and wouldn't ever expect that it is not one of the Array methods; but, as proved by Reed, it is not... – varocarbas Oct 01 '13 at 18:21
  • ... (http://stackoverflow.com/questions/19056967/vb-net-array-contains-method-does-not-work?noredirect=1#comment28165990_19056967 -> take a look at the comments for our conversation). Contains is a LINQ extension method and thus can only be used by arrays if a LINQ reference is set (what happens, by default, in VB.NET). Now I did an experiment with your code and it seems to follow the same rules: if you remove the reference to LINQ (in C# is easier), you would see that the method .Select stops being available for an array. Thus, Select is a LINQ extension method and thus I can call it LINQ :) – varocarbas Oct 01 '13 at 18:24
0

You can do it like this:

Dim num_str As String = ...
Dim str() As String = num_str.Split(",")
Dim result(str.Length - 1) As Integer
For i = 0 To str.Length - 1
    result(i) = str(i)
Next
srka
  • 792
  • 8
  • 20
0

I was just working on this for a client, and I found an elegant little piece of code that will replace your single string value and effectively convert it to integer (or more precisely "double") very easily, and can be used for "find and convert" just as easily. I wrote a bunch of these into a GPA calculator that changes the letter grade entered into the GPA number format for math conversion and display without anything else but these three lines and a little loop.

For m = 0 To UBound(myArray)
myArray(m) = Replace(myArray(m), "thisstring", 0.0)  'integer 0.0 can be any
Next m