Finding the string length of a integer in .NET

后端 未结 6 649
小鲜肉
小鲜肉 2020-12-28 18:12

In .NET what is the best way to find the length of an integer in characters if it was represented as a string?

e.g.

1 = 1 character
10 = 2 characters

6条回答
  •  无人及你
    2020-12-28 18:36

    You could use something like this:

            int integer = 100;
    
            int charachtersCount = 0;
            while (integer > 0)
            {
                integer = integer/10;
                charachtersCount++;
            }
    

    But do you really need to optimize this? I would actually prefer using string (looks much better):

    integer.ToString().Length
    

提交回复
热议问题