Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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# 如何将字符串替换为所有0_C#_String - Fatal编程技术网

C# 如何将字符串替换为所有0

C# 如何将字符串替换为所有0,c#,string,C#,String,我有一根线看起来像12345, 如何将所有字符替换为“0”,使其看起来像00000 我无法使用string.replace函数,因为我的字符串长度不是静态的,文本也不是静态的。我怎样才能更换它?我将给出一个小代码来清楚地解释它 string str = Console.ReadLine(); var ConvertedStr = str.Replace(str,"00000000"); Console.WriteLine(ConvertedStr); Console.ReadKey(); //o

我有一根线看起来像12345, 如何将所有字符替换为“0”,使其看起来像00000 我无法使用string.replace函数,因为我的字符串长度不是静态的,文本也不是静态的。我怎样才能更换它?我将给出一个小代码来清楚地解释它

string str = Console.ReadLine();
var ConvertedStr = str.Replace(str,"00000000");
Console.WriteLine(ConvertedStr);
Console.ReadKey();
//output:
//00000000

我要找的是ConvertedStr可以与str的长度相同,所有字符都是“0”。

您只需创建一个长度相同的新字符串,并使用类的正确名称仅插入指定的字符即可


@trix我有一个小应用程序,它要求在单击按钮后将数字字符串全部替换为0,数字可以是7位或5位。@CZA您也可以在其自身上新建:str=new string'0',str.Length;
string convertedString = new string('0', str.Length);