Ad

Split Strings In Flutter

- 1 answer

I have a bit dirty data to handle for a List<dynamic>.

"[[1 xJeans ($100.00)$100.00], [3 xshirt ($1.00)$3.00], [1 xBermudas ($2.00)$2.00], [4 xTie ($2.00)$8.00], [5 xJeans-Red ($23.00)$115.00], [1 xJeans-blue ($21.00)$21.00], [1 xShirt-Red ($12.00)$12.00], [2 xshirt-blue ($23.00)$46.00]]"

This is my List<Dynamic> and i've to split string from it. lets say for elementAt(0) we got [1 xJeans ($100.00)$100.00] so here 1 is Quantity, Jeans is my item, in brackets i have a single Unit Price and outside of bracket i have totalUnitPrice

So what i want to do is to split this data for every element and add it back to the array and want output like this

"[[1, Jeans , $100.00, $100.00], [3 ,shirt ,$1.00,$3.00], [1 ,Bermudas , $2.00, $2.00], [4 ,Tie , $2.00, $8.00], [5 ,Jeans-Red , $23.00, $115.00], [1 ,Jeans-blue , $21.00, $21.00], [1 ,Shirt-Red ,$12.00 , $12.00], [2 , shirt-blue , $23.00, $46.00]]"
Ad

Answer

You can iterate over the list and split the String into four String.

You can do something like this:

  var item ="1 xJeans (\$100.00)\$100.00";
  var first_step = item.split(" ");
  var quantity = first_step[0];
  var item_name = first_step[1].substring(1, first_step[1].length);

  var first_value = first_step[2].substring(first_step[2].indexOf("(")+1,first_step[2].indexOf(")"));
  var second_value = first_step[2].substring(first_step[2].indexOf(")")+1, first_step[2].length);

  print(quantity);
  print(item_name);
  print(first_value);
  print(second_value);

This will split the single inte into four usable values.

Ad
source: stackoverflow.com
Ad