StringReader格式化字符串
我已按照此 WordPress 教程进行操作效果很好。我使用了列表视图,当我尝试格式化字符串时,它无法识别 \t
(但可以识别 \n
)。它也无法识别 String.Format
等。
我是否可以使用制表符或类似的东西来格式化字符串?
干杯
编辑
for( i = 0; i < lstView.Items.Count;i++)
{
name = lstView.Items[i].Text;
state = lstView.Items[i].SubItems[1].Text;
country = lstView.Items[i].SubItems[2].Text;
line += name + "\t" + state + "\t" + country + "\n";
}
StringReader reader = new StringReader(line);
当使用行打印时,字符串被连接在一起,因此 \t 不起作用。不过,新行的 \n 确实有效。有谁知道我可以在不使用空格的情况下格式化字符串的任何方法。
结果是这样的
NameStateCountry
LongernameStateCountry
anotherNameAnotherStateAnotherCountry
我希望它们排列起来(就像在表格中一样),名称一列,说明另一列和国家/地区,然后是第三列
非常感谢任何建议
I have followed this Wordpress tutorial which works great. I have used a listview and when i try to format the string it doesn't recognise the \t
(but does recognise \n
). It also won't recognise String.Format
etc.
Is there anyway that I can format the string using tabs or something similar?
Cheers
EDIT
for( i = 0; i < lstView.Items.Count;i++)
{
name = lstView.Items[i].Text;
state = lstView.Items[i].SubItems[1].Text;
country = lstView.Items[i].SubItems[2].Text;
line += name + "\t" + state + "\t" + country + "\n";
}
StringReader reader = new StringReader(line);
When line is used to print the string is joined together so the \t doesn't work. The \n for a new line does work though. Does anyone know any way that I can format the string without using spaces.
The result is like this
NameStateCountry
LongernameStateCountry
anotherNameAnotherStateAnotherCountry
Where I would like them lined up (like in a table) with name one column, state another column and country then third
Any suggestions greatly appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,制表符丢失有点奇怪,但另一方面,如果各个字符串元素(名称、状态等)的长度不同,制表符可能会出现问题。
您可以使用
string.Format()
并使用固定列宽。为了获得良好的视觉输出,这将包括一个解析步骤来确定正确的列宽。
完成此操作后,使用类似的方法来使用空格而不是制表符。
编辑:发现您不想使用空格。
在这种情况下,您可能需要在打印算法本身中处理这个问题。您仍然可以使用选项卡分隔项目,然后对于每行将其拆分在选项卡上,每行创建一个项目(列)数组。
对于每个项目,使用带有合适的 X 位置偏移的 Graphics.DrawString() 来打印它。
请参阅 Graphics.DrawString 的文档。
Well, it is a bit odd that tabs are lost, but on the other hand, tabs will probably be problematic if the individual string elements (name, state etc.) varies in length.
What you could do instead is use
string.Format()
and use fixed column widths.To get nice visual output this would include a parse step to determine the correct column width.
When this is done, use something like this to use spaces instead of tabs.
EDIT: Saw that you did not want to use spaces.
In this case, you will probably need to handle this in the printing algorithm itself. You could still separate items with tabs, then for each line split it on tabs, creating an array of items (columns) per line.
For each item, print it using
Graphics.DrawString()
with a suitable X-position offset.See the documentation for Graphics.DrawString.