how to convert a string to an array of int string mystring= "1,2,3,4"; int myArrayOfInt=[1,2,3,4];
Asked
Active
Viewed 143 times
0
-
2[This question](http://stackoverflow.com/questions/2959161/linq-convert-string-to-int-array) may help you. – Romasz Mar 20 '14 at 14:05
-
Thx Romasz but I Can not use Array.Select in PCL !!! – Sam Mar 20 '14 at 14:10
2 Answers
0
This code would provide you array of int:
string source = "1,2,3,4";
var stringArray = source.Split(',');
var ArrayOfInt = stringArray.Select(x => Convert.ToInt32(x)).ToArray();

Pradeep Kesharwani
- 1,480
- 1
- 12
- 21
0
int[] myArrayOfInt = mystring.Split(',').Select(n => Convert.ToInt32(n)).ToArray();
but you cannot use linq in plc so:
var arrOfStr = mystring.Split(',');
int[] myArrayOfInt = new int()[arrOfStr.Length];
for(int i = 0; i < arrOfStr.Length; i++)
{
myArrayOfInt[i] = Convert.ToInt32(arrOfStr[i]);
}
something like this?

JP Hellemons
- 5,977
- 11
- 63
- 128
-
-
Well portability comes with limitations, so I do not think there is a more elegant way. – JP Hellemons Mar 20 '14 at 14:19
-
@Samissa You should be able to use LINQ in PCLs. What are the targets for your PCLs? – Daniel Plaisted Mar 20 '14 at 18:39
-