请有人帮我解析下面的这些示例字符串?我很难分割数据,而且数据需要在每个事件结束时添加回车
示例字符串:
L,030216,182748,00,FF,I,00,030216,182749,00,FF,I,00,030216,182750,00,FF,I,00
一批事件
预期产出:
L,030216,182748,00,FF,I,00
- 第一次活动L,030216,182749,00,FF,I,00
- 第二次活动L,030216,182750,00,FF,I,00
- 第3次活动答案 0 :(得分:1)
这应该可以解决问题,因为你的字符串里面不再有L's,而且逗号的位置始终是从批号开始的第六位。
class Program
{
static void Main(string[] args)
{
String batchOfevents = "L,030216,182748,00,FF,I,00,030216,182749,00,FF,I,00,030216,182750,00,FF,I,00,030216,182751,00,FF,I,00,030216,182752,00,FF,I,00,030216,182753,00,FF,I,00";
// take out the "L," to start processing by finding the index of the correct comma to slice.
batchOfevents = batchOfevents.Substring(2);
String output = "";
int index = 0;
int counter = 0;
while (GetNthIndex(batchOfevents, ',', 6) != -1)
{
counter++;
if (counter == 1){
index = GetNthIndex(batchOfevents, ',', 6);
output += "L, " + batchOfevents.Substring(0, index) + " - 1st event\n";
batchOfevents = batchOfevents.Substring(index + 1);
} else if (counter == 2) {
index = GetNthIndex(batchOfevents, ',', 6);
output += "L, " + batchOfevents.Substring(0, index) + " - 2nd event\n";
batchOfevents = batchOfevents.Substring(index + 1);
}
else if (counter == 3)
{
index = GetNthIndex(batchOfevents, ',', 6);
output += "L, " + batchOfevents.Substring(0, index) + " - 3rd event\n";
batchOfevents = batchOfevents.Substring(index + 1);
} else {
index = GetNthIndex(batchOfevents, ',', 6);
output += "L, " + batchOfevents.Substring(0, index) + " - " + counter + "th event\n";
batchOfevents = batchOfevents.Substring(index + 1);
}
}
output += "L, " + batchOfevents + " - " + (counter+1) + "th event\n";
Console.WriteLine(output);
}
public static int GetNthIndex(string s, char t, int n)
{
int count = 0;
for (int i = 0; i < s.Length; i++)
{
if (s[i] == t)
{
count++;
if (count == n)
{
return i;
}
}
}
return -1;
}
}
现在输出将采用您要求的格式,并且原始字符串已被分解。
注意: getNthIndex方法取自this old post。
答案 1 :(得分:1)
似乎是一个简单的问题。像这样简单的东西应该这样做:
string line = "L,030216,182748,00,FF,I,00,030216,182749,00,FF,I,00,030216,182750,00,FF,I,00";
string[] array = line.Split(',');
StringBuilder sb = new StringBuilder();
for(int i=0; i<array.Length-1;i+=6)
{
sb.AppendLine(string.Format("{0},{1} - {2} event",array[0],string.Join(",",array.Skip(i+1).Take(6)), "number"));
}
输出(sb.ToString()
):
L,030216,182748,00,FF,I,00 - number event
L,030216,182749,00,FF,I,00 - number event
L,030216,182750,00,FF,I,00 - number event
您所要做的就是处理增加序数的函数(第1个,第2个等),但那是easy to get。
答案 2 :(得分:0)
如果要将字符串拆分为多个字符串,则需要一组规则,
这是可实现的。在你的情况下,我会开始分裂完整
给定逗号,
的字符串,而不是循环中的元素。
循环中的所有字符串都将附加在StringBuilder
中。如果你的规则集
假设您需要换行,只需通过yourBuilder.Append('\r\n')
添加或使用AppendLine
。
修改的
使用此方法,您还可以轻松添加新的字符,例如L
或最后rd Event
答案 3 :(得分:0)
答案 4 :(得分:0)
试试以下内容:
string stream = "L,030216,182748,00,FF,I,00, 030216,182749,00,FF,I,00, 030216,182750,00,FF,I,00";
string[] lines = SplitLines(stream, "L", "I", ",");
此处SplitLines
函数用于检测任意格式的流中的可变长度事件:
string stream = "A;030216;182748 ;00;FF;AA;01; 030216;182749;AA;02";
string[] lines = SplitLines(batch, "A", "AA", ";");
分裂规则是:
- 输入流的所有元素由separator
(例如,
)分隔
- 每个事件都以特殊标记为界(例如L
和I
)
- 结束标记是事件序列的元素
static string[] SplitLines(string stream, string startSeq, string endLine, string separator) {
string[] elements = stream.Split(new string[] { separator }, StringSplitOptions.RemoveEmptyEntries);
int pos = 0;
List<string> line = new List<string>();
List<string> lines = new List<string>();
State state = State.SeqStart;
while(pos < elements.Length) {
string current = elements[pos].Trim();
switch(state) {
case State.SeqStart:
if(current == startSeq)
state = State.LineStart;
continue;
case State.LineStart:
if(++pos < elements.Length) {
line.Add(startSeq);
state = State.Line;
}
continue;
case State.Line:
if(current == endLine)
state = State.LineEnd;
else
line.Add(current);
pos++;
continue;
case State.LineEnd:
line.Add(endLine);
line.Add(current);
lines.Add(string.Join(separator, line));
line.Clear();
state = State.LineStart;
continue;
}
}
return lines.ToArray();
}
enum State { SeqStart, LineStart, Line, LineEnd };
答案 5 :(得分:0)
如果要将字符串拆分为多个字符串,则需要一组可实现的规则。在你的情况下,我会开始用给定的逗号分割整个字符串,而不是循环中的元素。循环中的所有字符串都将附加在StringBuilder中。如果你的规则集说你需要一个新行,只需通过yourBuilder.Append('\ r \ n')添加它或使用AppendLine。