I want to be able to parse a a six digit number and take each character and place in an array for example: "738593" -> d[1] = 7, d[2] = 3. d[3] = 8….etc
private void button2_Click(object sender, EventArgs e)
{
int[] d = new int[10];
d1 = Integer.parseInt(String.valueOf(s.charAt(0)));
d3 = Integer.parseInt(String.valueOf(s.charAt(1)));
d2 = Integer.parseInt(String.valueOf(s.charAt(2))); //JAVA LINE
...
}
I found a line from Java that does exactly what I want but I was looking around for a C# equivalent. Any ideas?
Try:
What you are doing here is iterating over each of the characters in the string, converting them to a stream of integers (
IEnumerable<int>) then making that stream into an array. This requires C# 3.0 and LINQ.Alternatively you could try:
which will throw an exception if the string being passed in is not a string of digits, but may have some performance penalty as for each character in the string a further string is made.
Both these method will work on digit strings of any size*.
*that can be stored in memory of course.