The following is a piece of code I have written
string arrayToConvert = "$78 89 12 78 89 12%";
string s2 = s1.Substring(1, s1.Length - (2));
There are 2 objectives to be achieved
- Removing the first and last character
- Transferring the string data into array of integers
I am able to remove the first and last character in a string and store it into a new one, no problem. The trouble is when I try to put the data into integer array. I tried using the following syntax to achieve the desired goal.
int[] ia = s1.Split(' ').Select(int.Parse).ToArray();
and
int n;
int[] ia = s1.Split(' ').Select(s => int.TryParse(s, out n) ? n : 0).ToArray();
The output obtained is "4455667788110121" which clearly are not the numbers in the input string. If this question is a duplicate, kindly point me in the right direction. Any help appreciated.
"78 89 12 78 89 12".Split(' ').Select(int.Parse).ToArray()returns an array with 6 elements whose values are 78, 89, etcarrayToConvertyet you trim a different string, s1. Finally, you try to split that s1 string, not the trimmed onearrayToConvertbut you try to substring and split a strings1. Is this defined somewhere else in your code?