How to split a string in C#

后端 未结 8 862
北海茫月
北海茫月 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条回答
  •  -上瘾入骨i
    2021-01-20 00:05

    You can use following code achieving this task

            string str = "List_1 fooo asdf List_2 bar fdsa XList_3 fooo bar";
            List l_lstValues = new List { "List_1", "XList_3", "List_2" };
    
            string[] strarr = str.Split(' ');
            string sKey, sValue;
            bool bFlag = false;
            sKey = sValue = string.Empty;
    
            var lstResult = new List>();
    
            foreach (string strTempKeys in l_lstValues)
            {
                bFlag = false;
                sKey = strTempKeys;
                sValue = string.Empty;
    
                foreach (string strTempValue in strarr)
                {
                    if (bFlag)
                    {
                        if (strTempValue != sKey && l_lstValues.Contains(strTempValue))
                        {
                            bFlag = false;
                            break;
                        }
                        else
                            sValue += strTempValue;
                    }
                    else if (strTempValue == sKey)
                        bFlag = true;
                }
    
                lstResult.Add(new KeyValuePair(sKey, sValue));                
            }
    

提交回复
热议问题