C# 拆分字符串,以便将所有+数据输入到一个列表中,并将-数据输入到另一个列表中

C# 拆分字符串,以便将所有+数据输入到一个列表中,并将-数据输入到另一个列表中,c#,C#,我的字符串如下所示,我希望以这样一种方式将所有+数据分隔到一个列表中,并将-分隔到另一个列表中 string InputStr = "DIM H + QUT OF PIP DIM - DIM L + XYZ - ABC"; 我的预期结果如下 string[] Positives = "DIM H QUT OF PIP DIM XYZ" string[] Negatives = "DIM L ABC" 当我将它与原始字符串匹配时,我想知道当找到匹配项时它是+还是- public bool Fi

我的字符串如下所示,我希望以这样一种方式将所有+数据分隔到一个列表中,并将-分隔到另一个列表中

string InputStr = "DIM H + QUT OF PIP DIM - DIM L + XYZ - ABC";
我的预期结果如下

string[] Positives = "DIM H QUT OF PIP DIM XYZ"
string[] Negatives = "DIM L ABC"
当我将它与原始字符串匹配时,我想知道当找到匹配项时它是+还是-

public bool FindMatchesInStringArray(string markup, string[] strArray, out string matchString)
{
    bool flag = false;
    matchString = string.Empty;
    for (int i = 0; i < strArray.Length; i++)
    {
        Match Match = Regex.Match(markup, strArray[i].Trim(), RegexOptions.IgnoreCase);
        if (Match.Success)
        {
            flag = true;
            matchString = Match.Value;
        }
    }
    return flag;
}

在标记中,我将发送DIM H或其他一些,以便在找到匹配项时,它应该给我+DIM H或-ABC。

我尝试的一种方法是创建两个StringBuilder,然后在char[]上循环,每次循环一个索引,检查是否有任何控制符号+或-,将本地状态设置为指向其中一个stringbuilder,然后将以下字符导入此stringbuilder

大概是这样的:

var sb, sb1, sb2;

for (c in string)
{
  if (c == '+') sb = sb1;
  else if (c == '-') sb = sb2;
  else sb.Append(c);
}

以下是一个非正则表达式解决方案:

static List<string> ListOfPositive = new List<string>();
static List<string> ListOfNegative = new List<string>();
static bool InitialPositiveCheck = false;
static void Main(string[] args)
{
    string InputStr = "DIM H + QUT OF PIP DIM - DIM L + XYZ - ABC";
    AddToLIst(InputStr);
    ListOfPositive.Reverse();
    ListOfNegative.Reverse();
    Console.WriteLine(string.Join("", ListOfPositive));
    Console.WriteLine(string.Join("", ListOfNegative));         
}

static void AddToLIst(string newstring)
{
    if (!(newstring.Trim().StartsWith("+")) && !InitialPositiveCheck)
    {
       newstring = "+ " + newstring;
       InitialPositiveCheck = true;
    }

    int indexOfPlus = newstring.LastIndexOf("+");
    int indexOfMinus = newstring.LastIndexOf("-");

    string str = "";
    if (indexOfMinus == -1 && indexOfPlus == -1)
        return;

    if (indexOfPlus < indexOfMinus)
    {
       str = newstring.Substring(indexOfMinus + 1);
       ListOfNegative.Add(str);
       str = newstring.Remove(indexOfMinus);
       AddToLIst(str);
    }
    else
    {
       str = newstring.Substring(indexOfPlus + 1);
       ListOfPositive.Add(str);
       str = newstring.Remove(indexOfPlus);
       AddToLIst(str);
    }
 }

好了,现在我们知道你想要什么了。问题在哪里?顺便说一下,StackOverflow不是免费的代码编写服务。提问前先阅读主题。是否总是在正面和负面之间交替?或者它们有时会是一行中同一个符号的多个?问题我已经解释了我正在做什么和期望做什么我有一个使用列表的非正则表达式解决方案。这就足够了吗?总是积极的和消极的。你可以用同样的方法做消极的功能,你可以一次做两件事。有一个肯定的列表和一个否定的列表。如果肯定=false,添加到否定列表而不是肯定列表。我总是使用SRP更好地重用,例如,如果从一个地方您只想要您选择的肯定。我对SRP更灵活——我认为这取决于函数的具体程度。切换标识符很聪明。我喜欢!
static List<string> ListOfPositive = new List<string>();
static List<string> ListOfNegative = new List<string>();
static bool InitialPositiveCheck = false;
static void Main(string[] args)
{
    string InputStr = "DIM H + QUT OF PIP DIM - DIM L + XYZ - ABC";
    AddToLIst(InputStr);
    ListOfPositive.Reverse();
    ListOfNegative.Reverse();
    Console.WriteLine(string.Join("", ListOfPositive));
    Console.WriteLine(string.Join("", ListOfNegative));         
}

static void AddToLIst(string newstring)
{
    if (!(newstring.Trim().StartsWith("+")) && !InitialPositiveCheck)
    {
       newstring = "+ " + newstring;
       InitialPositiveCheck = true;
    }

    int indexOfPlus = newstring.LastIndexOf("+");
    int indexOfMinus = newstring.LastIndexOf("-");

    string str = "";
    if (indexOfMinus == -1 && indexOfPlus == -1)
        return;

    if (indexOfPlus < indexOfMinus)
    {
       str = newstring.Substring(indexOfMinus + 1);
       ListOfNegative.Add(str);
       str = newstring.Remove(indexOfMinus);
       AddToLIst(str);
    }
    else
    {
       str = newstring.Substring(indexOfPlus + 1);
       ListOfPositive.Add(str);
       str = newstring.Remove(indexOfPlus);
       AddToLIst(str);
    }
 }
 DIM H  QUT OF PIP DIM  XYZ 
 DIM L  ABC