如何编写一个程序,输入一个由用户五位数组成的数字,将数字分成各自的数字,并将每个数字相互分开的数字打印出来。例如,如果用户键入数字42339
,则应打印程序
4 2 3 3 9
答案 0 :(得分:5)
string num = "42339";
string result = String.Join(" ", num.Select(c=>c));
修改强> 这部分只是为了好玩,我从评论中收集了一些备选方案,还添加了一些
string numstr = "42339";
string result = String.Join(" ", numstr.Select(c => c));
string result = String.Join(" ", (IEnumerable<char>) numstr);
string result = String.Join(" ", numstr.AsEnumerable());
string result = String.Join(" ", numstr.ToArray());
string result = String.Join(" ", numstr.ToCharArray());
string result = String.Join(" ", numstr.Where((_) => true));
string result = String.Join(" ", numstr.Where(char.IsDigit));
string result = String.Join(" ", numstr.Select(char.ToLower));
string result = string.Join(" ", Regex.Split(numstr, "")).Trim();
string result = numstr.Aggregate("", (s, c) => s += c + " ").Trim();
string result = Regex.Replace(numstr, ".", m => m.Value + " ").Trim();
string result = Regex.Replace(numstr, ".", "$0 ").Trim();
string result = Regex.Replace(numstr, "[^.]{0}", " ").Trim();
答案 1 :(得分:2)
int number = 42339;
var str = string.Join(" ", (IEnumerable<char>)number.ToString());
答案 2 :(得分:-1)
int number = 42339;
string result = String.Join(" ", number.ToString().ToCharArray());