我有号码:241233 如何将此号码拆分为单独的数字? 类似于: a = 2,b = 4,c = 1,d = 2,e = 3,f = 3 我可以在C#中做到这一点? (VisualStudio中2013) PS:数字改变每一次,但我知道它有6位如何将数字拆分为C中的数字#
回答
我认为您的解决方案是一个字典,一本字典有重点和价值每个项目。
可以存储每个项目(数),在一个字母,例如,A = 1。
首先,你建立这将您的数字转换在字典的方法,
public static void split(string number)
{
Dictionary<string, string> dict = new Dictionary<string, string>();
string s = "ABCDEF";
for (int i = 0; i < 6; i++)
{
dict.Add(s[i].ToString() , number[i].ToString()); // Here you add each char of the string ABCDEF, to the value from each char of the number. In this case A=2.
Console.WriteLine($"LETTER { s[i].ToString()} = { dict[s[i].ToString()] }"); // Here you print the values of your dictionary, if you want to call a value of your dictionary, you only have to say: dictionaryname[key], in this case, dictionaryname['A']
}
}
后,你打电话你的方法,你的方法的参数将是您要变换的数字。
static void Main(string[] args)
{
split("241233");
}
请一定要仔细阅读代码的评论//,
结果:
var collection = yourNumber.ToString().Select(c => Int32.Parse(c.ToString()));
//or var collection = yourNumber.ToString().Select(Char.GetNumericValue);
foreach(var num in collection)
Console.WriteLine(num);
以及如何显示它?什么是变量? –
我编辑帖子 –
@Mhai“数字每次变化”是什么意思?答案因您尝试做什么而异。 –
您可以在数字转换为字符串,并获得chararray,from the doc
String s = ... your 241233 as String;
var chars = s.ToCharArray();
Console.WriteLine("Original string: {0}", s);
Console.WriteLine("Character array:");
for (int ctr = 0; ctr < chars.Length; ctr++)
Console.WriteLine(" {0}: {1}", ctr, chars[ctr]);
我编辑文章 –
我会采取不同的方式,
没有必要打破它为字符数组。
你可以通过这个号码的长度循环。 然后取数字和模10。
12345%10 = 1234,(给你最后一位数字,5)。 1234%10 = 123,(给你4) 等等......
我觉得它更优雅的解决方案,它应该是有点快于它分成字符数组,并投它为int等,mod操作是原子操作,所以它比任何其他非原子操作都快。
,但如果我的号码每次都改变......你能给我代码吗?您的解决方案看起来非常好 –
今天晚些时候我会收到,我现在不在,我从我的移动应用程序回答 – Roi1981
我以后如何与您联系? –
- 1. C#拆分字符串 - 将字符串拆分为数组
- 2. 用Haskell将数字拆分为数字
- 3. 如何将数字拆分为组词?
- 4. C++如何将字符串拆分为字符串和整数?
- 5. 如何在Javascript中将数字拆分为数字?
- 6. 将列中的数字拆分为单独列中的数字
- 7. 关于将数字拆分为数千
- 8. 将r中的数据帧拆分为字符和数字
- 9. 如何在awk中将分隔字符串拆分为数组?
- 10. 将字符串拆分为字符数组 - Objective-C
- 11. 如何将字符串拆分为数字?
- 12. PHP:如何将一个数字拆分为多个字符串
- 13. 如何将字符串拆分为字符串数组?
- 14. 麻烦将字符串拆分为C中的数组
- 15. 在Prolog中将数字拆分为数字列表
- 16. 如何在js中将字符串('a | b \\ | c')拆分为数组('a','b | c'')?
- 17. C#中的字符串数组拆分
- 18. Android将字符串拆分为数组
- 19. 将长串拆分为字节数组
- 20. 将字符串拆分为双数组
- 21. 将字符串拆分为数组Shellllscript
- 22. 将CGPoint数字拆分为两个CGFloat
- 23. 如何将字符串拆分为整数数组?
- 24. 将数字n拆分为k个不同数字的总和
- 25. 如何将字符串拆分为字母字符串和数字字符串?
- 26. 如何使用JSTL函数将数字字符串拆分fn:split?
- 27. 如何拆分数字字符串android
- 28. 如何将字符串拆分为C中的标记?
- 29. 如何根据c中的长度将字符串拆分为数组#
- 30. 如何编写将字典拆分为字典数组的递归Python函数?
你尝试过什么到目前为止 – Nkosi
我投票关闭这个问题作为题外话,因为“gimme teh codez” – EJoshuaS