最简单的方法来分割在.NET换行符字符串? [英] Easiest way to split a string on newlines in .NET?

查看:183
本文介绍了最简单的方法来分割在.NET换行符字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个字符串分割成在.NET中,我知道了分裂的唯一办法是换行用的分割方法。但这不会让我(容易)在换行符分割,那么什么是做到这一点的最好方法是什么?

I need to split a string into newlines in .NET and the only way I know of to split strings is with the Split method. However that will not allow me to (easily) split on a newline, so what is the best way to do it?

推荐答案

要分裂一个字符串,你需要使用接受字符串数组过载:

To split on a string you need to use the overload that takes an array of strings:

string[] lines = theText.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

编辑:
如果要处理不同类型的换行文本,你可以用它来匹配多个字符串的能力。这将正确分裂两种类型的换行符,和preserve空行和间距于文:


If you want to handle different types of line breaks in a text, you can use the ability to match more than one string. This will correctly split on either type of line break, and preserve empty lines and spacing in the text:

string[] lines = theText.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);

这篇关于最简单的方法来分割在.NET换行符字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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