我正在处理一个与字符串相关的问题,其中也存在很少的数字。我的工作是迭代所有字符,识别这些数字并执行算术运算。最后返回修改后的字符串。但对于此操作,所有字符都更改为 Int。
谢谢
string str="Please Change 2015";
string str2=String.Join("", str.Select(x=> (x >= '0' && x <= '9') ?'9'-x: x).ToList());
Given Output: 8010810197115101326710497110103101327984
Required Output: Please Change 7984
最佳答案
或者只是
string str = "Please Change 2015";
string str2 = String.Join("", str.Select(x => char.IsDigit(x) ? (char)(9-(x-'0')+'0') : x).ToList());
输出
Please Change 7984
解释
-'0' // convert it from char to a number
+'0' // convert it back to a char
(char) // make sure we output characters again
关于c# - String 的 char 值改为 Int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52232188/