String Array For Each loop

TheCoder 91 Reputation points
2022-09-16T15:54:43.333+00:00

I have a string array being passed and I need to grab everything from it and call a routine based on the prefix, however, my foreach loop is only grabbing the last value passed in. How can I grab everything and pass it to the routine as value1;value2;value3.

the string array looks like this:
New_BMWX5;New_BMWX3;Used_LexusIS;Used_AudioQ8

I have the following:

dim enteredModels as String = car.Values  'this is the variable for the values being passed in  
dim cars as String()  
dim inventory as String = ""  
cars = enteredModels.Split(";")  
  
  
  
for each c as String in cars  
    if c.Contains("New") or (c.Contains("Used")) then  
      car.Values = c  
      inventory = String.Join(";", c.ToArray())  
  
     ' if the values of a prefix of new or used they call this  
      carInventory.SubCars(c, "#", grp.ReplaceChars)  
   else  
        car.Values = c  
      inventory = String.Join(";", c.ToArray())  
  
      ' if the values have no prefix, they call it like this  
      carInventory.SubCars(c, grp.ChangeFormat grp.ReplaceChars)  
   end if  
  
Next   

The issue I'm running into is that, it's only taking the last value from the string array, how can I call the correct routine and when I have values with and without prefixes in the string array?

Developer technologies VB
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 122.5K Reputation points
    2022-09-16T17:35:32.06+00:00

    Maybe you need something like this:

    Dim enteredModels = "New_BMWX5;New_BMWX3;Used_LexusIS;Used_AudioQ8"  
      
    Dim a = enteredModels.Split(";")  
      
    Dim values_with_prefix = String.Join(";", a.Where(Function(v) v.StartsWith("New_") OrElse v.StartsWith("Used_")))  
    Dim values_without_prefix = String.Join(";", a.Where(Function(v) Not (v.StartsWith("New_") OrElse v.StartsWith("Used_"))))  
    

    Then call your functions.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.