删除或修剪字符串末尾的回车符
可能的重复:
删除回车符和C# 中从字符串末尾换行
如何删除字符串末尾的回车符。 我已经用谷歌搜索了这个,但找不到我需要的代码。
这是我当前的代码:
return s.Replace("\n", "").Replace("\r", "").Replace("\r\n", "").Trim();
但当然所有出现的旧字符都会被替换。
我已经尝试过这些:
公共字符串修剪(字符串s) {
string[] whiteSpace = {"\r", "\n", "\r\n"};
foreach(string ws in whiteSpace)
{
if (s.EndsWith(ws))
{
s = s.Substring(0, s.Length - 1);
}
}
return s;
}
但对我不起作用,也许我错过了一些东西或者我的代码有问题
我也尝试使用正则表达式但我无法得到我需要的东西。
我从Word文档中获取字符串,然后将其传输到另一个文档。
我将不胜感激任何帮助。Tnx
Possible Duplicate:
Removing carriage return and new-line from the end of a string in c#
How do I remove the carriage return at the end of string.
I have already google out this but I cannot find the code I need.
This is my current code:
return s.Replace("\n", "").Replace("\r", "").Replace("\r\n", "").Trim();
but of course all the occurrence of the old character will be replaced.
I have tried these:
public string trim(string s)
{
string[] whiteSpace = {"\r", "\n", "\r\n"};
foreach(string ws in whiteSpace)
{
if (s.EndsWith(ws))
{
s = s.Substring(0, s.Length - 1);
}
}
return s;
}
but is not working for me, maybe I miss something or there's a wrong with my code
I have also try using regular expression but I can't get what I need.
I get the string from word document then will be transfered to another docs.
I'll appreciate any help.Tnx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您好,我在以下链接中找到了解决方案。所以功劳应该归于该作者。
示例
但是我已经自定义了着力提升示范能力。
看看这个。
Hi I found a solution in following link. So the credit should go to that author.
Example
However I have customized it to boost the demonstrative capability.
Check this out.
请参阅此答案: 从 C# 中的字符串末尾删除回车符和换行符
See this answer: Removing carriage return and new-line from the end of a string in c#