简体   繁体   English

如何获取包含指定数量空格的字符串?

[英]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. 在另一种语言中,我使用了函数SPACE(10) ,给我一串10个空格。 I want to convert this to C#.NET. 我想将其转换为C#.NET。

I have used string.Empty().PadLeft(10) . 我使用了string.Empty().PadLeft(10)

Is this correct? 这个对吗?

Simply use: 只需使用:

new String(' ', 10);

Here's the MSDN documentation: 这是MSDN文档:

String Constructor ( Char , Int32 ) String构造函数( CharInt32

Initializes a new instance of the String class to the value indicated by a specified Unicode character repeated a specified number of times. String类的新实例初始化为由指定的Unicode字符指示的值重复指定的次数。

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

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

You can use String Constructor (Char, Int32) 您可以使用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类的新实例初始化为由指定的Unicode字符指示的值重复指定的次数

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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