net4u net4u - 2 months ago 5
C# Question

How to split a string into individual letters

I have a string that represent a number like 1234567890 and I want to split this into individual digits, somehow as follows:

<div class="counter">
@if (Model.Count > 0)
char[] num = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }

string TotalCount = @Model.Count;
string[] TheDigit = TotalCount.Split(num);

foreach (var TheDigit in TotalCount)

How can I achieve this properly? Anyway my output must be something like this:

<li>1</li><li>2</li>...<li>9</li><li class="Zero">0</li>


First of all this is not view logic, IMHO, so it should not be done in the view but in the controller. Opinions may vary on this.

To the point, if you need to obtain the digits of a number, you can do it arithmetically or string-wise. String-wise you could do (assuming that normal digits occupy only a character, culture info, bla bla, turkish I, bla):

var x = "1234567";
foreach(var digit in x)