我需要在. net中将字符串分割为换行符,我所知道的分割字符串的唯一方法是使用split方法。然而,这将不允许我(容易)在换行上分裂,那么最好的方法是什么?
当前回答
其实很简单。
VB。NET:
Private Function SplitOnNewLine(input as String) As String
Return input.Split(Environment.NewLine)
End Function
C#:
string splitOnNewLine(string input)
{
return input.split(environment.newline);
}
其他回答
我目前在VB中使用这个函数(基于其他答案)。NET:
Private Shared Function SplitLines(text As String) As String()
Return text.Split({Environment.NewLine, vbCrLf, vbLf}, StringSplitOptions.None)
End Function
它首先尝试在平台本地换行符上进行分割,然后退回到每个可能的换行符。
到目前为止,我只在一个类中需要这个。如果这种情况发生了变化,我可能会将此设置为Public并将其移动到实用程序类,甚至可能将其设置为扩展方法。
下面是如何重新加入队列的方法:
Private Shared Function JoinLines(lines As IEnumerable(Of String)) As String
Return String.Join(Environment.NewLine, lines)
End Function
这里的示例非常棒,帮助我解决了当前的“挑战”,以一种更可读的方式分割rsa密钥。基于Steve Coopers的解决方案:
string Splitstring(string txt, int n = 120, string AddBefore = "", string AddAfterExtra = "")
{
//Spit each string into a n-line length list of strings
var Lines = Enumerable.Range(0, txt.Length / n).Select(i => txt.Substring(i * n, n)).ToList();
//Check if there are any characters left after split, if so add the rest
if(txt.Length > ((txt.Length / n)*n) )
Lines.Add(txt.Substring((txt.Length/n)*n));
//Create return text, with extras
string txtReturn = "";
foreach (string Line in Lines)
txtReturn += AddBefore + Line + AddAfterExtra + Environment.NewLine;
return txtReturn;
}
呈现一个具有33个字符宽度和引号的RSA-key是很简单的
Console.WriteLine(Splitstring(RSAPubKey, 33, "\"", "\""));
输出:
希望有人觉得它有用…
你应该能够很容易地分割你的字符串,就像这样:
aString.Split(Environment.NewLine.ToCharArray());
要拆分一个字符串,你需要使用一个字符串数组的重载:
string[] lines = theText.Split(
new string[] { Environment.NewLine },
StringSplitOptions.None
);
编辑: 如果要处理文本中不同类型的换行符,可以使用匹配多个字符串的功能。这将正确地拆分任意类型的换行,并保留文本中的空行和空格:
string[] lines = theText.Split(
new string[] { "\r\n", "\r", "\n" },
StringSplitOptions.None
);
我只是想加上我的二进制,因为这个问题的其他解决方案不属于可重用代码分类,不方便。
下面的代码块扩展了string对象,以便在处理字符串时可以使用它作为一个自然的方法。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Collections.ObjectModel;
namespace System
{
public static class StringExtensions
{
public static string[] Split(this string s, string delimiter, StringSplitOptions options = StringSplitOptions.None)
{
return s.Split(new string[] { delimiter }, options);
}
}
}
你现在可以从任何字符串中使用.Split()函数,如下所示:
string[] result;
// Pass a string, and the delimiter
result = string.Split("My simple string", " ");
// Split an existing string by delimiter only
string foo = "my - string - i - want - split";
result = foo.Split("-");
// You can even pass the split options parameter. When omitted it is
// set to StringSplitOptions.None
result = foo.Split("-", StringSplitOptions.RemoveEmptyEntries);
要在换行符上进行分割,只需传递“\n”或“\r\n”作为分隔符参数。
评论:如果微软能实现这个重载就太好了。
推荐文章
- 实体框架核心:在上一个操作完成之前,在此上下文中开始的第二个操作
- 如何为构造函数定制Visual Studio的私有字段生成快捷方式?
- 为什么Visual Studio 2015/2017/2019测试运行器没有发现我的xUnit v2测试
- 如何使用JSON确保字符串是有效的JSON。网
- Printf与std::字符串?
- AppSettings从.config文件中获取值
- 通过HttpClient向REST API发布一个空体
- 如何检查IEnumerable是否为空或空?
- 不区分大小写的“in”
- 自动化invokerrequired代码模式
- 我如何得到一个字符串的前n个字符而不检查大小或出界?
- 没有ListBox。SelectionMode="None",是否有其他方法禁用列表框中的选择?
- 在c#代码中设置WPF文本框的背景颜色
- 在c#中,什么是单子?
- 如何在PHP中截断字符串最接近于一定数量的字符?