从C#中的字符串末尾删除回车符和换行符 [英] Removing carriage return and new-line from the end of a string in c#

查看:2484
本文介绍了从C#中的字符串末尾删除回车符和换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从字符串末尾删除回车符(\r)和换行符(\n)?

How do I remove the carriage return character (\r) and the new line character(\n) from the end of a string?

推荐答案

这将从s的末尾修剪掉回车符和换行符的任何组合:

This will trim off any combination of carriage returns and newlines from the end of s:

s = s.TrimEnd(new char[] { '\r', '\n' });

编辑:或者,JP指出,您可以更简洁地将其拼写为:

Edit: Or as JP kindly points out, you can spell that more succinctly as:

s = s.TrimEnd('\r', '\n');

这篇关于从C#中的字符串末尾删除回车符和换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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