使用PadRight方法填充一个字符串 [英] Padding a string using PadRight method

查看:152
本文介绍了使用PadRight方法填充一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加空格在C#中的字符串的结束:

  Trip_Name1.PadRight(20);
 

也试过:

  Trip_Name1.PadRight(20,'');
 

这一切都不似乎工作。不过,我可以垫字符串以任何其它字符。为什么呢?

我本来应该更具体,这里到处是code:

  lnk_showmatch_1.Text = u_trip.Trip_Name1.PadRight(20'');
 

解决方案

字符串是不变,它们不能被改变。 PadRight 返回字符串填充的新实例,不会改变之一,它是由调用。你需要的是这样的:

  Trip_Name1 = Trip_Name1.PadRight(20'');
 

这个的StackOverflow的问题,为什么字符串是不可变

修改

  

这一切都不似乎工作。不过,我可以垫字符串以任何其它字符。

您实际上重新分配给它像上面的例子吗?如果是这样的话 - 那么没有更多的细节,我只能认为以下的:

  1. 如果你是存储这在数据库和检索它,用正确的设置有些数据库裁剪给你。
  2. 您有逻辑在其他地方被修剪白色空间。当处理用户输入打交道是常见的。

编辑2

  

我本来应该更具体

我要带的基础上,你正在处理的HTML / ASP.NET命名约定的胡乱猜测。在大多数情况下,在HTML中 - 空白被折叠。例如:

 < D​​IV>< A>的Hello World< / A>< / DIV>
< D​​IV>< A>的Hello World< / A>< / DIV>
 

两者的,因为空格被倒塌标签将呈现相同的。如果你确实正在与HTML - 这可能是你的理由,为什么填充适用于所有其它字符。如果你这样做渲染标记的查看源代码 - 它包含了额外的空格

如果您想保留空格,试试你的元素称为空格在应用CSS样式,并将其设置为 pre 。例如:

 <风格=白色空间:pre>的Hello World< / A>
 

这将导致空白是preserved。请记住,使用空格这样有缺点。浏览器不使它们相同的,等我不会用这种布局的目的。请考虑使用CSS和类似最小宽度代替。

I am trying to add spaces to end of a string in C#:

Trip_Name1.PadRight(20);

Also tried:

Trip_Name1.PadRight(20,' ');

None of this seems to work. However I can pad the string with any other character. Why?

I should have been more specific, here is full code:

lnk_showmatch_1.Text = u_trip.Trip_Name1.PadRight(20,' ');

解决方案

String are immutable, they cannot be changed. PadRight returns a new instance of the string padded, not change the one it was called from. What you want is this:

Trip_Name1 = Trip_Name1.PadRight(20,' ');

There is a great discussion on this StackOverflow question as to why strings are immutable.

EDIT:

None of this seems to work. However I can pad the string with any other character.

Are you actually re-assigning it like the example above? If that is the case - then without more detail I can only think of the following:

  1. If you are storing this in a database and retrieving it, some databases with the correct settings may "Trim" for you.
  2. You have logic somewhere else that is trimming the white-spaces. This is common when dealing with user input.

EDIT 2:

I should have been more specific

I'm going to take a wild guess based on your naming conventions that you are dealing with HTML / ASP.NET. In most cases, in HTML - white space is collapsed. For example:

<div><a>Hello           World</a></div>
<div><a>Hello World</a></div>

Both of the a tags will render the same because the white-space is being collapsed. If you are indeed working with HTML - that is likely your reason and why the padding works for all other characters. If you do a view-source of the markup rendered - does it contain the additional white spaces?

If you wanted to keep the whitespaces, try applying a CSS style on your element called white-space and set it to pre. For example:

<a style="white-space:pre">hello     world      </a>

That will cause the white-space to be preserved. Keep in mind that using white space like this has disadvantages. Browsers don't render them identically, etc. I wouldn't use this for layout purposes. Consider using CSS and something like min-width instead.

这篇关于使用PadRight方法填充一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆