Split String, Convert Tolist<Int>() in One Line

Split string, convert ToListint() in one line

var numbers = sNumbers?.Split(',')?.Select(Int32.Parse)?.ToList();

Recent versions of C# (v6+) allow you to do null checks in-line using the null-conditional operator

Convert string to Liststring in one line?

List<string> result = names.Split(new char[] { ',' }).ToList();

Or even cleaner by Dan's suggestion:

List<string> result = names.Split(',').ToList();

How to split() a delimited string to a ListString

string.Split() returns an array - you can convert it to a list using ToList():

listStrLineElements = line.Split(',').ToList();

Note that you need to import System.Linq to access the .ToList() function.

Convert string[] to int[] in one line of code using LINQ

Given an array you can use the Array.ConvertAll method:

int[] myInts = Array.ConvertAll(arr, s => int.Parse(s));

Thanks to Marc Gravell for pointing out that the lambda can be omitted, yielding a shorter version shown below:

int[] myInts = Array.ConvertAll(arr, int.Parse);

A LINQ solution is similar, except you would need the extra ToArray call to get an array:

int[] myInts = arr.Select(int.Parse).ToArray();

Convert List of String to List of int Dart

You need to add a toList() to the end of the second line.

List <String> lstring = <String>["1", "2"];
List <int> lint = lstring.map(int.parse).toList();

This will do it.

Split string into Listint ignore none int values

List<int> arrCMs = strMyList.Split(',')
.Select(possibleIntegerAsString => {
int parsedInteger = 0;
bool isInteger = int.TryParse(possibleIntegerAsString , out parsedInteger);
return new {isInteger, parsedInteger};
})
.Where(tryParseResult => tryParseResult.isInteger)
.Select(tryParseResult => tryParseResult.parsedInteger)
.ToList();

The first Select in the above example returns an anonymous type that describes the result of int.TryParse - that is, whether it was a valid integer, and if so, what the value was.

The Where clause filters out those that weren't valid.

The second Select then retrieves the parsed values from the strings that were able to be parsed.

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>();

How can I split and trim a string into parts all on one line?

Try

List<string> parts = line.Split(';').Select(p => p.Trim()).ToList();

FYI, the Foreach method takes an Action (takes T and returns void) for parameter, and your lambda return a string as string.Trim return a string

Foreach extension method is meant to modify the state of objects within the collection. As string are immutable, this would have no effect

Hope it helps ;o)

Cédric

How to convert Liststring to Listint?

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


Related Topics



Leave a reply



Submit