Home > Enterprise >  String split c# but ignore some cases
String split c# but ignore some cases

Time:01-18

I am trying to split some txt file according to one string. In another words, text file includes many "COMPONENT" word and I want to split the text depend on the "COMPONENT" word.

Here is my code :

string[] splitComponents(string a)
{
    return a.Split(new String[] { "COMPONENT" }, StringSplitOptions.None);
}

It is working well but it detects some cases for example " COMPONENT_ " and split the text also which is not proper in my case. How can I ignore this situation? I only want to split "COMPONENT" word not "COMPONENT_". Thank you in advance.

CodePudding user response:

You could do a regex split on the pattern COMPONENT(?!_):

string[] splitComponents(string a)
{
    return Regex.Split(a, @"COMPONENT(?!_)");
}
  •  Tags:  
  • Related