Ant*_*ell 56 c# string algorithm int
将以下int参数转换为字符串,而不使用任何本机toString功能.
Run Code Online (Sandbox Code Playgroud)public string integerToString(int integerPassedIn){ //Your code here }
既然一切都继承自Object
并且Object
有一个ToString()
方法,你如何int
在string
不使用本机ToString()
方法的情况下将a 转换为a ?
字符串连接的问题在于它会调用ToString()
链直到它击中一个或命中Object
该类.
如何在不使用C#的情况下将整数转换为字符串ToString()
?
p.s*_*w.g 72
像这样的东西:
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;
}
Run Code Online (Sandbox Code Playgroud)
更新:这是另一个版本,它更短,应该执行得更好,因为它消除了所有字符串连接,有利于操作固定长度的数组.它支持最多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);
}
Run Code Online (Sandbox Code Playgroud)
joe*_*joe 22
这是我一直使用的解决方案:
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;
}
Run Code Online (Sandbox Code Playgroud)
这看起来很复杂,但具有以下特点:
我并不是真的相信连接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());
Run Code Online (Sandbox Code Playgroud)
整数从最低有效数字处理到最高有效数字.使用模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());
}
Run Code Online (Sandbox Code Playgroud)
我的第一个版本使用a StringBuilder
来创建字符数组中的字符串,但是获取字符串"out of the" StringBuilder
需要调用名为的方法ToString
.显然,这个方法不会对字符串进行任何int转换,这对我来说就是这个问题.
但是为了证明你可以在不调用的情况下创建一个字符串,ToString
我已经切换到使用string
构造函数,我也认为与使用构造函数相比更高效StringBuilder
.
如果ToString
禁止以任何形式使用,则不能使用文档中所示的字符串连接string.Concat
:
该方法通过调用arg0和arg1的无参数ToString方法来连接arg0和arg1; 它不会添加任何分隔符.
所以执行s += '1'
会打电话'1'.ToString()
.但对我来说这并不重要.重要的部分是如何将int转换为字符串.
归档时间: |
|
查看次数: |
15224 次 |
最近记录: |