0

I have a list<string> that contains a list of elements like this

element_a, element_b, element_c, element_d

and that is easy enough to deal with,

 for(String elements : ELEMENTS_ARRAY) {   }

but I on occasion have a list<string> like this

element_a, element_b 3, element_c, element_d

I am trying to figure how how to deal with an array where a value contains a space followed by a number and then return the element_b with the number as two separate values.

7
  • 2
    Apex methods only support a single return value. Can you break down your use case for us with some more specifics as to what you need?
    – David Reed
    Apr 25, 2019 at 20:21
  • What have you tried so far? I'd start by looking at the available String methods. Seems like the various substring[Before|After|Between]() methods, split(), and/or isNumeric() could be applicable to your problem. Apr 25, 2019 at 20:22
  • Currently looking into using a regular expression to find the value, but and looked into isNumeric, but that didn't work as the string itself would be alphanumberic Apr 25, 2019 at 20:25
  • It's be used in a for loop to generate part of a soap request, but I need it to split the string with the number into a separate quantity field Apr 25, 2019 at 20:27
  • did you checked -splitByCharacterType()? Apr 25, 2019 at 20:27

1 Answer 1

1
Map<String, Integer> tempMap = new Map<String, Integer>();
for(String element : ELEMENTS_ARRAY) {  
      if(element.contains(’ ’){
           tempMap.put(element.split(’ ’).get(0), Integer.valueOf(element.split(’ ’).get(1));
      }
 }

Then return the map and use the key as the element and its value as the number

1
  • That's perfect and works so cleanly Apr 25, 2019 at 20:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .