How to split a string in C#

后端 未结 8 861
北海茫月
北海茫月 2021-01-19 23:22

I\'m having a string like

\"List_1 fooo asdf List_2 bar fdsa XList_3 fooo bar\"

and a List like



        
8条回答
  •  不思量自难忘°
    2021-01-20 00:13

    You could do something like below:

    string sampleStr = "List_1 fooo asdf List_2 bar fdsa XList_3 fooo bar";
    string[] splitStr = 
       sampleStr.Split(l_lstValues.ToArray(), StringSplitOptions.RemoveEmptyEntries);
    

    EDIT: Modified to print the fragments with list word as well

    Assumption: There is no ':' in sampleStr

    foreach(string listWord in l_lstValues)
    {
        sampleStr = sampleStr.Replace(listWord, ':'+listWord);
    }
    string[] fragments = sampleStr.Split(':');
    

提交回复
热议问题