简体   繁体   English

如何在不使用库函数的情况下在C#中将Int转换为String?

[英]How to convert Int To String in C# without using library functions?

What is the most effective code to convert int to string without using a native convert function. 不使用本机转换功能将int转换为字符串的最有效代码是什么?

 public static void Main(string[] args)
    {
        string y = Convert(990);
        Console.WriteLine(y);
        Console.ReadLine(); 
    }


    public static string Convert(int x)
    {

         char[] Str = new char[20];
         int i = 0;
         while (x != 0)
         {
             Str[i++] = x % 10 + '0';
             x = x / 10;
         }

         return Str.ToString();// How do I handle this without the native function?
    }

The above doesnt seem to work. 以上似乎不起作用。 Please advise. 请指教。

You are adding the least significant digits to the start of the string. 您正在将最低有效数字添加到字符串的开头。 You'll need to reverse the string, or add the digits in reverse order, or insert them rather than append them. 您需要反转字符串,或者以相反的顺序添加数字,或者插入而不是附加数字。

And you'll need to do some casting. 而且您需要进行一些转换。 For example, here's one way to do it: 例如,这是一种实现方法:

Str[i++] = (char)(x % 10 + '0');

Finally, you cannot use ToString() like that. 最后,您不能像这样使用ToString() You want this: 你要这个:

return new string(Str, 0, i);

Although StringBuilder might be more suitable. 尽管StringBuilder可能更合适。

And note that your code won't work properly for negative input values, or for zero. 请注意,您的代码对于负输入值或零将无法正常工作。

So, here's a version that handles all of that: 因此,这是处理所有这些问题的版本:

public static string Convert(int x)
{
    if (x == 0)
    {
        return "0";
    }
    StringBuilder sb = new StringBuilder();
    string prefix = "";
    if (x < 0)
    {
        prefix = "-";
        x = -x;
    }
    while (x != 0)
    {
        sb.Insert(0, (char)(x % 10 + '0'));
        x = x / 10;
    }
    return prefix + sb.ToString();
}

This is pretty ugly though! 不过这很丑!

It might be easier to construct your result using a StringBuilder rather than a char array: 使用StringBuilder而不是char数组构造结果可能会更容易:

public static string Convert(int x)
{
     StringBuilder sb = new StringBuilder();

     while (x != 0)
     {
         sb.Insert(0, (char)(x % 10 + '0'));
         x = x / 10;
     }

     return sb.ToString();
}

The Insert(0, x) allows you to insert the more significant digits at the front. Insert(0, x)允许您在前面插入更高的数字。

Here's a solution without a String constructor or a .ToString() call. 这是一个没有String构造函数或.ToString()调用的解决方案。 This one handles negative numbers. 这个处理负数。 It's a bit too 'clever' for my taste, but it's an academic exercise anyway... 就我的口味而言,这太“聪明”了,但无论如何,这是一项学术练习...

void Main()
{
    Console.WriteLine(Convert(-5432));
}

String Convert(int i)
{
    return String.Join("",Digits(i).Reverse());
}

IEnumerable<char> Digits(int i)
{
    bool neg = false;
    if(i==0) {yield return '0'; yield break;}
    if(i<0) { i = -i; neg = true;}
    while (i!=0)
    {
        char digit = (char)(i % 10 + '0');
        i = i / 10;
        yield return digit;
    }
    if(neg) yield return '-';

    yield break;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM