增加数字(字符串)中的所有数字一步 [英] Increase all digits in a number(string) one step

查看:54
本文介绍了增加数字(字符串)中的所有数字一步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人有一个很好的算法/方法来增加所有数字一个

步骤,例如123123到234234和4444到5555和999999到000000?


祝你好运

Niklas

Does anyone have a nice algorithm/method to increase all digits one
step, e.g. 123123 to 234234 and 4444 to 5555 and 999999 to 000000?

Best regards
Niklas

推荐答案

Niklas Engfelt写道:
Niklas Engfelt wrote:
有没有人有一个很好的算法/方法来增加所有数字一步,例如123123到234234和4444到5555和999999到000000?
Does anyone have a nice algorithm/method to increase all digits one
step, e.g. 123123 to 234234 and 4444 to 5555 and 999999 to 000000?




嗯,这取决于你的意思一步到位。在一种方法中调用?

非常容易:


静态字符串IncDigits(字符串文本)

{

char [] chars = text.ToCharArray();


for(int i = 0; i< chars.Length; i ++)

{

char c = chars [i];

if(c> =''0''&& c< =''9'')

{

//不仅仅是c ++ - 包裹9-> 0

c =(char)((((c - ''0' ')+ 1)%10)+''0'');

chars [i] = c;

}

}

返回新字符串(字符);

}


Jon



Well, it depends what you mean by "in one step". In one method call?
Very easily:

static string IncDigits(string text)
{
char[] chars = text.ToCharArray();

for (int i=0; i < chars.Length; i++)
{
char c = chars[i];
if (c >= ''0'' && c <=''9'')
{
// Not just c++ - wrap 9->0
c = (char)((((c-''0'')+1)%10)+''0'');
chars[i]=c;
}
}
return new string(chars);
}

Jon


你可以使用.ToCharArray()转换,在
a时循环char数组1,转换为int,加1转换回来。最后将

整个char数组转换回字符串?

只是大脑倾倒

Can you convert using .ToCharArray(), loop through the char array 1 at
a time, convert to int, add 1 convert back. Finally convert back the
whole char array to a string?
Just brain dumping


是的,他说了什么!

yeah, what he said!


这篇关于增加数字(字符串)中的所有数字一步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆