Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C将数字用作字符串_C# - Fatal编程技术网

C# C将数字用作字符串

C# C将数字用作字符串,c#,C#,我需要做一个程序,从用户那里得到一个3位数的数字,然后打印一个6位数的数字 例:输入:123 输出:112233我假设您实际上可以得到任何数字作为输入,而不是三位数 您可以这样做: Console.WriteLine(String.Concat(Console.ReadLine().Select(c => c.ToString() + c.ToString()))); string input = Console.ReadLine(); StringBuilder output = ne

我需要做一个程序,从用户那里得到一个3位数的数字,然后打印一个6位数的数字 例:输入:123
输出:112233

我假设您实际上可以得到任何数字作为输入,而不是三位数

您可以这样做:

Console.WriteLine(String.Concat(Console.ReadLine().Select(c => c.ToString() + c.ToString())));
string input = Console.ReadLine();
StringBuilder output = new StringBuilder(input.Length * 2);

foreach (char c in input)
{
   output.append(c);
   output.append(c);
}

Console.WriteLine(output.ToString());

使用任意位数执行此操作的通用方法如下所示:

Console.WriteLine(String.Concat(Console.ReadLine().Select(c => c.ToString() + c.ToString())));
string input = Console.ReadLine();
StringBuilder output = new StringBuilder(input.Length * 2);

foreach (char c in input)
{
   output.append(c);
   output.append(c);
}

Console.WriteLine(output.ToString());
您可以使用Enumerable。在LINQ查询中重复:

public static String duplicateChars(IEnumerable<Char> input, int factor)
{
    var chars = from c in input
                from cc in Enumerable.Repeat(c, factor)
                select cc;
    return new String(chars.ToArray());
}
演示:

或简称:

new String("123".SelectMany(c => Enumerable.Repeat(c, 2)).ToArray());
您也可以使用regex

var reg=Regex.Replace123,@\d,@$&$&;

或者作为一种方法

公共静态字符串重复CharsString项 { 返回Regex.Replaceterm、@\d、@$&$&; }