You can use the string
constructor:
Console.WriteLine(new string('.', 10));
Initializes a new instance of the String class to the value indicated by a specified Unicode character repeated a specified number of times.
سؤال
Possible Duplicate:
Is there an easy way to return a string repeated X number of times?
If I want to display a dot 10 times in Python, I could either use this:
print ".........."
or this
print "." * 10
How do I use the second method in C#? I tried variations of:
Console.WriteLine("."*10);
but none of them worked. Thanks.
المحلول
You can use the string
constructor:
Console.WriteLine(new string('.', 10));
Initializes a new instance of the String class to the value indicated by a specified Unicode character repeated a specified number of times.
نصائح أخرى
You can use one of the 'string' constructors, like so:
Console.WriteLine(new string('.', 10));
I would say the most straight forward answer is to use a for
loop. This uses less storage.
for (int i = 0; i < 10; i++)
Console.Write('.');
Console.WriteLine();
But you can also allocate a string that contains the repeated characters. This involves less typing and is almost certainly faster.
Console.WriteLine(new String('.', 10));
try something like this
string print = "";
for(int i = 0; i< 10 ; i++)
{
print = print + ".";
}
Console.WriteLine(print);