How to Convert List<String> to List<Int>

How to convert Liststring to Listint?

listofIDs.Select(int.Parse).ToList()

How to convert Liststring[] into Listint[]

In one line

arrayIntList = arrayStringList.Select(x => x.Select(int.Parse).ToArray()).ToList();

Convert Liststring to Listint in C#

Instead of using LINQ you can use List<T>.ConvertAll<TOutput>(...)

List<int> intList = stringList.ConvertAll(int.Parse);

How to convert ListString to ListInteger?

Nope, there's no other way.

But casting is not possible in this case, you need to do use Integer.parseInt(stringValue).

List<String> listStrings = ... 
List<Integer> listIntegers = new ArrayList<Integer>(listStrings.size());
for(String current:listStrings){
listIntegers.add(Integer.parseInt(current));
}

Convert all strings in a list to int

Given:

xs = ['1', '2', '3']

Use map then list to obtain a list of integers:

list(map(int, xs))

In Python 2, list was unnecessary since map returned a list:

map(int, xs)

Convert ListString to ListInteger directly

No, you need to loop over the array

for(String s : strList) intList.add(Integer.valueOf(s));

How to convert a list of numbers in a String to a list of int in dart

Just base on following steps:

  1. remove the '[]'
  2. splint to List of String
  3. turn it to a int List

Sth like this:

  List<int> list =
value.replaceAll('[', '').replaceAll(']', '')
.split(',')
.map<int>((e) {
return int.tryParse(e); //use tryParse if you are not confirm all content is int or require other handling can also apply it here
}).toList();

Update:

You can also do this with the json.decode() as @pskink suggested if you confirm all content is int type, but you may need to cast to int in order to get the List<int> as default it will returns List<dynamic> type.

eg.

List<int> list = json.decode(value).cast<int>();

Converting ListInteger to ListString

As far as I know, iterate and instantiate is the only way to do this. Something like (for others potential help, since I'm sure you know how to do this):

List<Integer> oldList = ...
/* Specify the size of the list up front to prevent resizing. */
List<String> newList = new ArrayList<>(oldList.size());
for (Integer myInt : oldList) {
newList.add(String.valueOf(myInt));
}

Converting a ListString to Listint

It looks like your numbers are in a single string separated by spaces if so you can use Linq:

List<int> allNumbers = numbers.Split(' ').Select(int.Parse).ToList();

If you really have a List<string> numbers already simply:

List<int> allNumbers = numbers.Select(int.Parse).ToList();

Or finally, if each string may contain multiple numbers separated by spaces:

List<int> allNumbers  = numbers.SelectMany(x=> x.Split(' ')).Select(int.Parse).ToList();


Related Topics



Leave a reply



Submit