質問

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);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top