C# 修剪字符串中的换行符时出现问题
我有一个从 Xceed 数据网格填充的字符串,如下所示:
study = row.Cells["STUDY_NAME"].Value.ToString().TrimEnd (Environment.NewLine.ToCharArray());
但是,当我将研究字符串传递到程序的另一部分时,它会抛出一个错误,因为该字符串仍然显示为:“PI3K1003\n”
我也有尝试过:
TrimEnd('\r', '\n');
有人有什么想法吗?
谢谢。
I have a string that is populated from an Xceed datagrid, as such:
study = row.Cells["STUDY_NAME"].Value.ToString().TrimEnd (Environment.NewLine.ToCharArray());
However, when I pass the study string into another part of the program, it throws an error as the string is still showing as : "PI3K1003\n "
I have also tried:
TrimEnd('\r', '\n');
Anybody any ideas?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不起作用,因为该字符串末尾有空格,请使用
isn't working because you have a space at the end of that string, use
该字符串的问题是末尾没有
\n
。末尾有一个' '
(空白),因此\n
不会被修剪。所以我会尝试
TrimEnd( ' ', '\n', '\r' );
The problem in that string is that you do not have a
\n
at the end. You have a' '
(blank) at the end, and therefore\n
does not get trimmed.So I would try
TrimEnd( ' ', '\n', '\r' );
使用替换而不是 TrimEnd,在这种情况下,您永远不会遇到“空间问题”问题;)
但 TrimEnd 始终是解决方案:
Use replace instead of TrimEnd, in this case you never run into a 'space problem' problem ;)
But TrimEnd is always solution: