![](/img/trans.png)
[英]How to convert Int To String in C# without using library functions?
[英]How do I convert an Int to a String in C# without using ToString()?
將以下 int 參數轉換為字符串,而不使用任何本機 toString 功能。
public string integerToString(int integerPassedIn){ //Your code here }
由於所有東西都繼承自Object
並且Object
有一個ToString()
方法,你如何在不使用本機ToString()
方法的情況下將int
轉換為string
?
字符串連接的問題在於它會在鏈上調用ToString()
直到它命中一個或命中Object
類。
如何在不使用ToString()
情況下在 C# 中將整數轉換為字符串?
像這樣的東西:
public string IntToString(int a)
{
var chars = new[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
var str = string.Empty;
if (a == 0)
{
str = chars[0];
}
else if (a == int.MinValue)
{
str = "-2147483648";
}
else
{
bool isNegative = (a < 0);
if (isNegative)
{
a = -a;
}
while (a > 0)
{
str = chars[a % 10] + str;
a /= 10;
}
if (isNegative)
{
str = "-" + str;
}
}
return str;
}
更新:這是另一個更短且性能更好的版本,因為它消除了所有字符串連接,有利於操作固定長度的數組。 它最多支持 16 個基數,但很容易將其擴展到更高的基數。 它可能可以進一步改進:
public string IntToString(int a, int radix)
{
var chars = "0123456789ABCDEF".ToCharArray();
var str = new char[32]; // maximum number of chars in any base
var i = str.Length;
bool isNegative = (a < 0);
if (a <= 0) // handles 0 and int.MinValue special cases
{
str[--i] = chars[-(a % radix)];
a = -(a / radix);
}
while (a != 0)
{
str[--i] = chars[a % radix];
a /= radix;
}
if (isNegative)
{
str[--i] = '-';
}
return new string(str, i, str.Length - i);
}
這是我一直使用的解決方案:
public static string numberBaseChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static string IntToStringWithBase(int n, int b) {
return IntToStringWithBase(n, b, 1);
}
public static string IntToStringWithBase(int n, int b, int minDigits) {
if (minDigits < 1) minDigits = 1;
if (n == 0) return new string('0', minDigits);
string s = "";
if ((b < 2) || (b > numberBaseChars.Length)) return s;
bool neg = false;
if ((b == 10) && (n < 0)) { neg = true; n = -n; }
uint N = (uint)n;
uint B = (uint)b;
while ((N > 0) | (minDigits-- > 0)) {
s = numberBaseChars[(int)(N % B)] + s;
N /= B;
}
if (neg) s = "-" + s;
return s;
}
這看起來很復雜,但具有以下特點:
我並不真正相信連接operator +
調用ToString
,但如果確實如此,您可以通過執行以下操作來避免這兩者:
if (a == 0) return "0";
/* Negative maxint doesn't have a corresponding positive value, so handle it
* as a special case. Thanks to @Daniel for pointing this out.
*/
if (a == 0x80000000) return "-2147483648";
List<char> l = new List<char>();
bool negative = false;
if (a < 0)
{
negative = true;
a *= -1;
}
while (a > 0)
{
l.Add('0' + (char)(a % 10));
a /= 10;
}
if (negative) l.Add('-');
l.Reverse();
return new String(l.ToArray());
整數從最低有效位到最高有效位進行處理。 使用模 10 (%10) 計算單個數字,然后將其添加到字符值“0”。 這導致字符“0”、“1”、...、“9”之一。
這些數字被壓入堆棧,因為它們在處理時必須以相反的順序呈現(最高有效數字到最低有效數字)。 這樣做而不是重復將數字添加到字符串可能會更有效,但由於數字的數量非常少,您必須執行基准測試才能確定。
需要一些額外的處理來處理非正數。
public string IntToString(int a) {
if (a == 0)
return "0";
if (a == int.MinValue)
return "-2147483648";
var isNegative = false;
if (a < 0) {
a = -a;
isNegative = true;
}
var stack = new Stack<char>();
while (a != 0) {
var c = a%10 + '0';
stack.Push((char) c);
a /= 10;
}
if (isNegative)
stack.Push('-');
return new string(stack.ToArray());
}
我的第一個版本使用StringBuilder
創建從字符數組,但得到的字符串“走出”的字符串StringBuilder
要求方法的調用命名ToString
。 顯然,這個方法沒有做任何 int 到字符串的轉換,這對我來說就是這個問題的意義所在。
但是為了證明你可以在不調用ToString
情況下創建一個字符串,我已經切換到使用string
構造函數,我也認為它比使用StringBuilder
更有效。
如果禁止任何形式的ToString
,則不能使用string.Concat
文檔中string.Concat
字符串連接:
該方法通過調用arg0和arg1的無參數ToString方法連接arg0和arg1; 它不添加任何分隔符。
所以執行s += '1'
將調用'1'.ToString()
。 但對我來說這並不重要。 重要的部分是如何將 int 轉換為字符串。
針對較短的版本,以及使用Math.DivRem
:
string IntToString(int a)
{
if (a == int.MinValue)
return "-2147483648";
if (a < 0)
return "-" + IntToString(-a);
if (a == 0)
return "0";
var s = "";
do
{
int r;
a = Math.DivRem(a, 10, out r);
s = new string((char)(r + (int)'0'), 1) + s;
}
while (a > 0);
return s;
}
使用new string(..., 1)
構造函數只是滿足 OP 要求的一種方法,即不得對任何東西調用ToString
。
您可以像這樣將任何數字轉換為字符
byte = (char)(byte)(digit+48)
幻數48
是字符0
的 ASCII 值,它們在 ASCII 表中是連續的,因此您只需添加數字即可獲得 ASCII 表中的相應值。 並且您可以使用模數運算符以迭代方式獲取整數中的數字%
借用 pswg 的一般結構,您將得到
public string IntToString(int a) {
var str = string.Empty;
bool isNegative = false;
if (a < 0) {
isNegative = true;
a = -a;
}
do {
str = (char)(byte)((a % 10) + 48) + str;
a /= 10;
} while(a > 0);
return isNegative ? '-' + str : str
}
這是我使用迭代和遞歸以及運行時分析對其的看法。
public static class IntegerToString
{
static char[] d = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
public static string Iteration(int num, int radix = 10)
{
if (num == 0) return "0";
if (num < 0) return "-" + Iteration(Math.Abs(num));
var r = new List<char>();
while (num > 0)
{
r.Insert(0, d[num % radix]);
num /= radix;
}
return new string(r.ToArray());
}
public static string Recursion(int num, int radix = 10)
{
if (num == 0) return "0";
if (num < 0) return "-" + Recursion(Math.Abs(num));
return (num > radix - 1 ? Recursion(num / radix) : "") + d[num % radix];
}
}
以下是這兩種方法與我電腦上的標准ToString()
相比的運行時分析。
50 runs of 100000 items per set
Running Time:
Iteration: 00:00:02.3459591 (00:00:00.0469191 avg)
Recursion: 00:00:02.1359731 (00:00:00.0427194 avg)
Standard : 00:00:00.4271253 (00:00:00.0085425 avg)
Ratios:
| Iter | Rec | Std
-----+------+------+-----
Iter | 1.00 | 0.91 | 0.18
Rec | 1.10 | 1.00 | 0.20
Std | 5.49 | 5.00 | 1.00
結果表明迭代和遞歸方法的運行速度比標准ToString()
方法慢 5.49 和 5.00 倍。
這是我用於分析的代碼:
class Program
{
static void Main(string[] args)
{
var r = new Random();
var sw = new System.Diagnostics.Stopwatch();
var loop = new List<long>();
var recr = new List<long>();
var std = new List<long>();
var setSize = 100000;
var runs = 50;
Console.WriteLine("{0} runs of {1} items per set", runs, setSize);
for (int j = 0; j < runs; j++)
{
// create number set
var numbers = Enumerable.Range(1, setSize)
.Select(s => r.Next(int.MinValue,
int.MaxValue))
.ToArray();
// loop
sw.Start();
for (int i = 0; i < setSize; i++)
IntegerToString.Iteration(numbers[i]);
sw.Stop();
loop.Add(sw.ElapsedTicks);
// recursion
sw.Reset();
sw.Start();
for (int i = 0; i < setSize; i++)
IntegerToString.Recursion(numbers[i]);
sw.Stop();
recr.Add(sw.ElapsedTicks);
// standard
sw.Reset();
sw.Start();
for (int i = 0; i < setSize; i++)
numbers[i].ToString();
sw.Stop();
std.Add(sw.ElapsedTicks);
}
Console.WriteLine();
Console.WriteLine("Running Time:");
Console.WriteLine("Iteration: {0} ({1} avg)",
TimeSpan.FromTicks(loop.Sum()),
TimeSpan.FromTicks((int)loop.Average()));
Console.WriteLine("Recursion: {0} ({1} avg)",
TimeSpan.FromTicks(recr.Sum()),
TimeSpan.FromTicks((int)recr.Average()));
Console.WriteLine("Standard : {0} ({1} avg)",
TimeSpan.FromTicks(std.Sum()),
TimeSpan.FromTicks((int)std.Average()));
double lSum = loop.Sum();
double rSum = recr.Sum();
double sSum = std.Sum();
Console.WriteLine();
Console.WriteLine("Ratios: \n" +
" | Iter | Rec | Std \n" +
"-----+------+------+-----");
foreach (var div in new[] { new {n = "Iter", t = lSum},
new {n = "Rec ", t = rSum},
new {n = "Std ", t = sSum}})
Console.WriteLine("{0} | {1:0.00} | {2:0.00} | {3:0.00}",
div.n, lSum / div.t, rSum / div.t, sSum / div.t);
Console.ReadLine();
}
public static string integerToString(int integerPassedIn)
{
if (integerPassedIn == 0) return "0";
var negative = integerPassedIn < 0;
var res = new List<char>();
while(integerPassedIn != 0)
{
res.Add((char)(48 + Math.Abs(integerPassedIn % 10)));
integerPassedIn /= 10;
}
res.Reverse();
if (negative) res.Insert(0, '-');
return new string(res.ToArray());
}
如果這是c ++,那么:
public string IntToString(int a){
char rc[20];
int x = a;
if(a < 0) x = -x;
char *p = rc + 19;
*p = 0;
do
*--p = (x % 10) | '0';
while(x /= 10);
if(a < 0) *--p = '-';
return string(p);
}
遞歸:
public static string integerToString(int integerPassedIn)
{
ICollection<char> res = new List<char>();
IntToStringRecusion(integerPassedIn, res);
if (integerPassedIn < 0) res.Add('-');
return new string(res.Reverse().ToArray()).PadLeft(1,'0');
}
static void IntToStringRecusion(int integerPassedIn, ICollection<char> array)
{
if (integerPassedIn == 0) return;
array.Add((char)(48 + Math.Abs(integerPassedIn % 10)));
IntToStringRecusion(integerPassedIn / 10, array);
}
簡單的餡餅:
string s = 5 + ""
//s = "5"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.