什么是一个快速的方法来强制CRLF在C#/。NET? [英] What is a quick way to force CRLF in C# / .NET?

查看:443
本文介绍了什么是一个快速的方法来强制CRLF在C#/。NET?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你会如何规范化字符串中的所有新行序列一种类型?

我在寻找使他们所有CRLF的电子邮件(MIME文件)的目的。理想情况下,这将被包裹在一个静态方法,执行速度非常快,并没有使用常规EX pressions(因为换行符,回车等的差异是有限的)。也许甚至还有首创置业的方法我已经忽略了?

假设:给这个有点更多的思考之后,我认为这是一个安全的假设说,CR的要么是单机或CRLF序列的一部分。也就是说,如果你看到CRLF那么你知道所有的CR的可以被删除。否则,很难说有多少行应该出来的东西,如\ r \ñ\ñ\ r。

解决方案

  input.Replace(\ r \ N,\ N),更换(\ r, \ N),更换(\ N,\ r \ N)
 

这将工作,如果输入仅包含一种类型的换行符 - 无论是CR,或LF,或CR + LF

How would you normalize all new-line sequences in a string to one type?

I'm looking to make them all CRLF for the purpose of email (MIME documents). Ideally this would be wrapped in a static method, executing very quickly, and not using regular expressions (since the variances of line breaks, carriage returns, etc. are limited). Perhaps there's even a BCL method I've overlooked?

ASSUMPTION: After giving this a bit more thought, I think it's a safe assumption to say that CR's are either stand-alone or part of the CRLF sequence. That is, if you see CRLF then you know all CR's can be removed. Otherwise it's difficult to tell how many lines should come out of something like "\r\n\n\r".

解决方案

input.Replace("\r\n", "\n").Replace("\r", "\n").Replace("\n", "\r\n")

This will work if the input contains only one type of line breaks - either CR, or LF, or CR+LF.

这篇关于什么是一个快速的方法来强制CRLF在C#/。NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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