简体   繁体   中英

How can I get a string containing a defined number of spaces?

In another language, I've used the function SPACE(10) , to give me a string of 10 spaces. I want to convert this to C#.NET.

I have used string.Empty().PadLeft(10) .

Is this correct?

Simply use:

new String(' ', 10);

Here's the MSDN documentation:

String Constructor ( Char , Int32 )

Initializes a new instance of the String class to the value indicated by a specified Unicode character repeated a specified number of times.

这个string构造函数有一个重载

var spaces = new string(' ', 10);

You can use String Constructor (Char, Int32)

Initializes a new instance of the String class to the value indicated by a specified Unicode character repeated a specified number of times .

string s = new string(' ', 10);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM