gpt4 book ai didi

c# - 拆分具有奇怪模式的字符串

转载 作者:太空宇宙 更新时间:2023-11-03 15:58:27 25 4
gpt4 key购买 nike

我需要帮助来拆分一组具有相当奇怪模式的字符串。

示例数据:

List<string> input = new List<string>();
input.Add("Blue Code \n 03 ID \n 05 Example \n Sky is blue");
input.Add("Green Code\n 01 ID\n 15");
input.Add("Test TestCode \n 99 \n Testing is fun");

预期输出:

For input[0]:
string part1 = "Blue"
string part2 = "Code \n 03"
string part3 = "ID \n 05"
string part4 = "Example \n Sky is blue"

For input[1]:
string part1 = "Green"
string part2 = "Code\n 01"
string part3 = "ID\n 15"

For input[2]:
string part1 = "Test"
string part2 = "TestCode \n 99"
string part3 = "\n Testing is fun"

Edited with one more example:

"038  038\n 0004  049.0\n 0006"

预期输出:

"038"
"038\n 0004"
"049.0\n 0006"

简而言之,我什至不知道如何描述模式...似乎我需要在“\n”之前的第一个字符串(作为键)作为新字符串的一部分,但是最后一个输入 [2] 与其他 2 个的模式略有不同。另外,请注意空格,它们非常不一致。

我知道这是不可能的,但如果有人能想出如何处理这些数据,请告诉我。

Updated: I think I can forget about solving this... When I actually take a look at the database in detail, I just found out that there are NOT only \n, it can be... anything, including |a |b |c (from a-z, A-Z), \a \b \c (from a-z, A-Z). Manually re-entering the data could be much more easier...

最佳答案

我会说模式是:

List<string> input = new List<string>();
input.Add("Blue Code \n 03 ID \n 05 Example \n Sky is blue");
input.Add("Green Code\n 01 ID\n 15");
input.Add("Test TestCode \n 99 \n Testing is fun");

foreach(string text in input)
{
string rest = text;
//1 Take first word
string part1 = rest.Split(' ')[0];
rest = rest.Skip(part1.Length).ToString();
//while rest contains (/n number)

while (rest.Contains("\n"))
{
//Take until /n number
int index = rest.IndexOf("\n");
string partNa = rest.Take(index).ToString();
string temp = rest.Skip(index).ToString();
string partNb = temp.Split(' ')[0];
int n;
if (int.TryParse("123", out n))
{
string partN = partNa + partNb;
rest = rest.Skip(partN.Length).ToString();
}
}

//Take rest
string part3 = rest;
}

它可能会写得更优化一些,但你明白了。

关于c# - 拆分具有奇怪模式的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22460183/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com