I want to convert a List<string>
to a List<int>
.
Here is my code:
void Convert(List<string> stringList) { List<int> intList = new List<int>(); for (int i = 0; i < stringList.Count; i++) { intList.Add(int.Parse(stringList[i])); } )
This basic method to convert a list of strings to a list of integers uses three steps: Create an empty list with ints = [] . Iterate over each string element using a for loop such as for element in list . Convert the string to an integer using int(element) and append it to the new integer list using the list.
Use int() function to Convert list to int in Python. This method with a list comprehension returns one integer value that combines all elements of the list.
Pass the List<String> as a parameter to the constructor of a new ArrayList<Object> . List<Object> objectList = new ArrayList<Object>(stringList);
Instead of using LINQ you can use List<T>.ConvertAll<TOutput>(...)
List<int> intList = stringList.ConvertAll(int.Parse);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With