C# 如何在VB.NET中使用空格作为填充字符形成初始化控件的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/514484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use a blank space as padding character to form a string that initializes a control in VB.NET
提问by
I want to right pad my string with blank spaces. The following function works for all characters except the blank.
我想用空格右填充我的字符串。以下函数适用于除空白以外的所有字符。
myString.PadRight(50,"x")
Edit:
编辑:
myString.PadRight(50) does pad a string with blank spaces, but when this string is used in the following statement, the blank padding is gone.
myString.PadRight(50) 确实用空格填充字符串,但是当在以下语句中使用此字符串时,空白填充消失了。
myCheckBox.Text = myString.PadRight(50)
回答by tvanfosson
I think if you just omit the character specifier it will pad on the right with spaces.
我想如果你只是省略字符说明符,它会在右边填充空格。
var newString = myString.PadRight(50);
See the doc reference. It sure seems like it should work by specifying the space character as well, though. I don't know why it would treat it any differently than any other Unicode character.
请参阅文档参考。不过,它似乎也应该通过指定空格字符来工作。我不知道为什么它会以与任何其他 Unicode 字符不同的方式对待它。
回答by Joel Coehoorn
It sounds like this is for an asp.net page, in which case the problem is with how html treats whitespace. If you look at your source, the spaces are probably rendered to the client. However, the browser won't show them. Use a CSS style for the element instead to set the width or render
characters.
这听起来像是针对 asp.net 页面,在这种情况下,问题在于 html 如何处理空格。如果您查看源代码,则空间可能会呈现给客户端。但是,浏览器不会显示它们。改用元素的 CSS 样式来设置宽度或呈现
字符。
回答by okutane
Do you really need that padding? Maybe you just want need to set alignment of text in your control?
你真的需要那个填充物吗?也许您只需要在控件中设置文本对齐方式?
回答by Renze de Waal
How do you know the blank padding is wrong? I do not experience this when I try your code in VS 2005 with a dwindows.forms checkbox.
你怎么知道空白填充是错误的?当我在带有 dwindows.forms 复选框的 VS 2005 中尝试您的代码时,我没有遇到这种情况。
I guess that either the custom control removes the spaces or your observation is incorrect.
我猜想要么是自定义控件删除了空格,要么是您的观察不正确。