[英]split strings into many strings by newline?
i have incoming data that needs to be split into multiple values...ie. 我有传入的数据,需要分成多个值...即。
2345\\n564532\\n345634\\n234 234543\\n1324 2435\\n
2345 \\ n564532 \\ n345634 \\ n234 234543 \\ n1324 2435 \\ n
The length is inconsistent when i receive it, the spacing is inconsistent when it is present, and i want to analyze the last 3 digits before each \\n. 当我收到它时,长度是不一致的,当它存在时间距是不一致的,我想分析每个\\ n之前的最后3位数。 how do i break off the string and turn it into a new string?
如何断开字符串并将其转换为新字符串? like i said, this round, it may have 3 \\n commands, next time, it may have 10, how do i create 3 new strings, analyze them, then destroy them before the next 10 come in?
就像我说的,这一轮,它可能有3个\\ n命令,下一次,它可能有10个,我如何创建3个新字符串,分析它们,然后在接下来的10个进入之前销毁它们?
string[] result = x.Split('\r');
result = x.Split(splitAtReturn, StringSplitOptions.None);
string stringToAnalyze = null;
foreach (string s in result)
{
if (s != "\r")
{
stringToAnalyze += s;
}
else
{
how do i analyze the characters here?
}
}
You could use the string.Split method . 您可以使用string.Split方法 。 In particular I suggest to use the overload that use a string array of possible separators.
特别是我建议使用使用可能分隔符的字符串数组的重载。 This because splitting on the newline character poses an unique problem.
这是因为拆分换行符会带来一个独特的问题。 In you example all the newline chars are simply a '\\n', but for some OS the newline char is '\\r\\n' and if you can't rule out the possibility to have the twos in the same file then
在你的例子中,所有换行符都只是一个'\\ n',但是对于某些操作系统,换行符字符是'\\ r \\ n',如果你不能排除在同一个文件中有两个字符串的可能性那么
string test = "2345\n564532\n345634\n234 234543\n1324 2435\n";
string[] result = test.Split(new string[] {"\n", "\r\n"}, StringSplitOptions.RemoveEmptyEntries);
Instead if your are certain that the file contains only the newline separator allowed by your OS then you could use 相反,如果您确定该文件仅包含您的操作系统允许的换行符分隔符,那么您可以使用
string test = "2345\n564532\n345634\n234 234543\n1324 2435\n";
string[] result = test.Split(new string[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);
The StringSplitOptions.RemoveEmptyEntries allows to capture a pair of consecutive newline or an ending newline as an empty string. StringSplitOptions.RemoveEmptyEntries允许将一对连续换行符或结束换行符捕获为空字符串。
Now you can work on the array examining the last 3 digits of every string 现在,您可以处理数组,检查每个字符串的最后3位数
foreach(string s in result)
{
// Check to have at least 3 chars, no less
// otherwise an exception will occur
int maxLen = Math.Min(s.Length, 3);
string lastThree = s.Substring(s.Length - maxLen, maxLen);
... work on last 3 digits
}
Instead, if you want to work only using the index of the newline character without splitting the original string, you could use string.IndexOf in this way 相反,如果你只想使用换行符的索引而不拆分原始字符串,你可以用这种方式使用string.IndexOf
string test = "2345\n564532\n345634\n234 234543\n1324 2435\n";
int pos = -1;
while((pos = test.IndexOf('\n', pos + 1)) != -1)
{
if(pos < test.Length)
{
string last3part = test.Substring(pos - 3, 3);
Console.WriteLine(last3part);
}
}
string lines = "2345\n564532\n345634\n234 234543\n1324 2435\n";
var last3Digits = lines.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
.Select(line => line.Substring(line.Length - 3))
.ToList();
foreach(var my3digitnum in last3Chars)
{
}
last3Digits : [345, 532, 634, 543, 435]
last3Digits:
[345, 532, 634, 543, 435]
345,532,634,543,435 [345, 532, 634, 543, 435]
This has been answered before, check this thread: Easiest way to split a string on newlines in .NET? 之前已经回答过这个问题,请查看这个主题: 在.NET中换行符串的最简单方法吗?
An alternative way is using StringReader: 另一种方法是使用StringReader:
using (System.IO.StringReader reader = new System.IO.StringReader(input)) {
string line = reader.ReadLine();
}
Your answer is: theStringYouGot.Split('\\n');
你的答案是:
theStringYouGot.Split('\\n');
where you get an array of strings to do your processing for. 在哪里可以获得一系列字符串来进行处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.