question

TheCoder-8889 avatar image
0 Votes"
TheCoder-8889 asked JiachenLiMFST-9349 commented

String Array For Each loop

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?


dotnet-visual-basic
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi
Can you post all the code (unless very large), as there is a lot of confusion/redundncy in the code you posted.

0 Votes 0 ·

Hi @TheCoder-8889 ,
May I know if you have got any chance to check the answer?
It helps you separating the strings with and without prefixes.

0 Votes 0 ·

1 Answer

Viorel-1 avatar image
1 Vote"
Viorel-1 answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.