i need help splitting '123456' into ['1','2','3','4','5','6']
-
Hey how can i split a string into individual characters... my input string is always a set of numbers like '15323' and i want to split it up and add all the numbers together so i end up with a total of 14 (from before). Roman
You can use array syntax to iterate over the characters in a string, use int.TryParse to turn them into numbers, and add them. Something like this string input = "12324590234" int total = 0; foreach(char c in input) // oh yeah, I reckon this will work, too { int next = 0; if(int.TryParse(c.ToString(), out next)) { total += next; } } MessageBox.Show(next.ToString()); That's off the cuff, but it should be close.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
Hey how can i split a string into individual characters... my input string is always a set of numbers like '15323' and i want to split it up and add all the numbers together so i end up with a total of 14 (from before). Roman
if you are building that string dynamically try to add a character like',' in between, so your string should be"1,2,3,4,5". If your string is this way then we could easily convert it to an array using the split method... example: string str1="1,2,3,4,5"; then str1.split(',') ....later you can use a for loop and add the elements in the array.....
Gautham
-
Hey how can i split a string into individual characters... my input string is always a set of numbers like '15323' and i want to split it up and add all the numbers together so i end up with a total of 14 (from before). Roman
Hi,
char[] chars = "123456".ToCharArray();
Robert -
Hey how can i split a string into individual characters... my input string is always a set of numbers like '15323' and i want to split it up and add all the numbers together so i end up with a total of 14 (from before). Roman
hi , hope the below codesnippet will satisfy your needs string s ="123456"; int total=0; for(int i =0 ; i <s.Length ;i++> ) { int m = Convert.ToInt32(s[i].ToString()); total += m; } Thanks Saravanan R