如何删除"\ r \ n"从C#中的字符串?我可以使用正则表达式吗? [英] How can I remove "\r\n" from a string in C#? Can I use a regular expression?

查看:127
本文介绍了如何删除"\ r \ n"从C#中的字符串?我可以使用正则表达式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试保留来自ASP.NET文本区域的字符串.我需要剥离回车换行符,然后将剩下的所有内容分解为50个字符的字符串数组.

I am trying to persist string from an ASP.NET textarea. I need to strip out the carriage return line feeds and then break up whatever is left into a string array of 50 character pieces.

我到目前为止有这个

var commentTxt = new string[] { };
var cmtTb = GridView1.Rows[rowIndex].FindControl("txtComments") as TextBox;
if (cmtTb != null)
  commentTxt = cmtTb.Text.Length > 50
      ? new[] {cmtTb.Text.Substring(0, 50), cmtTb.Text.Substring(51)}
      : new[] {cmtTb.Text};

工作正常,但我没有删除CrLf字符.如何正确执行此操作?

It works OK, but I am not stripping out the CrLf characters. How do I do this correctly?

推荐答案

您可以使用正则表达式,是的,但是一个简单的字符串.Replace()可能就足够了.

You could use a regex, yes, but a simple string.Replace() will probably suffice.

 myString = myString.Replace("\r\n", string.Empty);

这篇关于如何删除"\ r \ n"从C#中的字符串?我可以使用正则表达式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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