将 int 转换为字符串?

发布于 2024-09-06 08:47:34 字数 67 浏览 8 评论 0原文

如何在 C# 中将 int 数据类型转换为 string 数据类型?

How can I convert an int datatype into a string datatype in C#?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

无人问我粥可暖 2024-09-13 08:47:34
string myString = myInt.ToString();
string myString = myInt.ToString();
澉约 2024-09-13 08:47:34
string a = i.ToString();
string b = Convert.ToString(i);
string c = string.Format("{0}", i);
string d = 
quot;{i}";
string e = "" + i;
string f = string.Empty + i;
string g = new StringBuilder().Append(i).ToString();
string a = i.ToString();
string b = Convert.ToString(i);
string c = string.Format("{0}", i);
string d = 
quot;{i}";
string e = "" + i;
string f = string.Empty + i;
string g = new StringBuilder().Append(i).ToString();
千秋岁 2024-09-13 08:47:34

以防万一你想要二进制表示并且你仍然从昨晚的聚会中喝醉了:

private static string ByteToString(int value)
{
    StringBuilder builder = new StringBuilder(sizeof(byte) * 8);
    BitArray[] bitArrays = BitConverter.GetBytes(value).Reverse().Select(b => new BitArray(new []{b})).ToArray();
    foreach (bool bit in bitArrays.SelectMany(bitArray => bitArray.Cast<bool>().Reverse()))
    {
        builder.Append(bit ? '1' : '0');
    }
    return builder.ToString();
}

注意:关于没有很好地处理字节序的事情......


如果你不介意为了速度而牺牲一点内存,你可以使用下面的方法生成一个包含预先计算的字符串值的数组:

static void OutputIntegerStringRepresentations()
{
    Console.WriteLine("private static string[] integerAsDecimal = new [] {");
    for (int i = int.MinValue; i < int.MaxValue; i++)
    {
        Console.WriteLine("\t\"{0}\",", i);
    }
    Console.WriteLine("\t\"{0}\"", int.MaxValue);
    Console.WriteLine("}");
}

Just in case you want the binary representation and you're still drunk from last night's party:

private static string ByteToString(int value)
{
    StringBuilder builder = new StringBuilder(sizeof(byte) * 8);
    BitArray[] bitArrays = BitConverter.GetBytes(value).Reverse().Select(b => new BitArray(new []{b})).ToArray();
    foreach (bool bit in bitArrays.SelectMany(bitArray => bitArray.Cast<bool>().Reverse()))
    {
        builder.Append(bit ? '1' : '0');
    }
    return builder.ToString();
}

Note: Something about not handling endianness very nicely...


If you don't mind sacrificing a bit of memory for speed, you can use below to generate an array with pre-calculated string values:

static void OutputIntegerStringRepresentations()
{
    Console.WriteLine("private static string[] integerAsDecimal = new [] {");
    for (int i = int.MinValue; i < int.MaxValue; i++)
    {
        Console.WriteLine("\t\"{0}\",", i);
    }
    Console.WriteLine("\t\"{0}\"", int.MaxValue);
    Console.WriteLine("}");
}
饭团 2024-09-13 08:47:34
int num = 10;
string str = Convert.ToString(num);
int num = 10;
string str = Convert.ToString(num);
情绪少女 2024-09-13 08:47:34

任何对象的 ToString 方法都应该返回该对象的字符串表示形式。

int var1 = 2;

string var2 = var1.ToString();

The ToString method of any object is supposed to return a string representation of that object.

int var1 = 2;

string var2 = var1.ToString();
撕心裂肺的伤痛 2024-09-13 08:47:34

进一步@Xavier的回应,这是一个进行速度比较的页面 在几种不同的方法之间进行从 100 次迭代到 21,474,836 次迭代的转换。

这之间似乎几乎有联系:

int someInt = 0;
someInt.ToString(); //this was fastest half the time
//and
Convert.ToString(someInt); //this was the fastest the other half the time

Further on to @Xavier's response, here's a page that does speed comparisons between several different ways to do the conversion from 100 iterations up to 21,474,836 iterations.

It seems pretty much a tie between:

int someInt = 0;
someInt.ToString(); //this was fastest half the time
//and
Convert.ToString(someInt); //this was the fastest the other half the time
恋你朝朝暮暮 2024-09-13 08:47:34
string str = intVar.ToString();

在某些情况下,您不必使用 ToString()

string str = "hi " + intVar;
string str = intVar.ToString();

In some conditions, you do not have to use ToString()

string str = "hi " + intVar;
聊慰 2024-09-13 08:47:34

或者:

string s = Convert.ToString(num);

or:

string s = Convert.ToString(num);
挖鼻大婶 2024-09-13 08:47:34
using System.ComponentModel;

TypeConverter converter = TypeDescriptor.GetConverter(typeof(int));
string s = (string)converter.ConvertTo(i, typeof(string));
using System.ComponentModel;

TypeConverter converter = TypeDescriptor.GetConverter(typeof(int));
string s = (string)converter.ConvertTo(i, typeof(string));
忆离笙 2024-09-13 08:47:34

没有一个答案提到 < code>ToString() 方法 可以应用于整数表达式

Debug.Assert((1000*1000).ToString()=="1000000");

甚至可以应用于整数文字

Debug.Assert(256.ToString("X")=="100");

尽管像这样的整数文字通常被认为是糟糕的编码样式(幻数)在某些情况下此功能可能很有用...

None of the answers mentioned that the ToString() method can be applied to integer expressions

Debug.Assert((1000*1000).ToString()=="1000000");

even to integer literals

Debug.Assert(256.ToString("X")=="100");

Although integer literals like this are often considered to be bad coding style (magic numbers) there may be cases where this feature is useful...

慕烟庭风 2024-09-13 08:47:34
string s = "" + 2;

你可以做一些好事,例如:

string s = 2 + 2 + "you" 

结果将是:

“4个你”

string s = "" + 2;

and you can do nice things like:

string s = 2 + 2 + "you" 

The result will be:

"4 you"

夕色琉璃 2024-09-13 08:47:34

如果您从数据集中获取

string newbranchcode = (Convert.ToInt32(ds.Tables[0].Rows[0]["MAX(BRANCH_CODE)"]) ).ToString();

if you're getting from a dataset

string newbranchcode = (Convert.ToInt32(ds.Tables[0].Rows[0]["MAX(BRANCH_CODE)"]) ).ToString();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文