Split using delimiter except when delimiter is escaped

前端 未结 5 1288
我寻月下人不归
我寻月下人不归 2020-12-11 05:49

I\'m reading clipboard data coming from excel using

var stream = (System.IO.Stream) ( Forms.Clipboard.GetDataObject() ).GetData( Forms.DataFormats.CommaSepara

5条回答
  •  情歌与酒
    2020-12-11 06:36

    From your input example, we can see that there are three "unwanted" sequences of characters:

    \"
    \",
    ,\"
    

    So, add all these sequences to the input array for the Split method:

    string[] result = clipData.Split(new[] { @",\""", @"\"",", @"\""" }, 
        StringSplitOptions.None);
    

    This will give you an array containing a few empty elements. If that is a problem, use StringSplitOptions.RemoveEmptyEntries instead of StringSplitOptions.None:

    string[] result = clipData.Split(new[] { @",\""", @"\"",", @"\""" }, 
        StringSplitOptions.RemoveEmptyEntries);
    

提交回复
热议问题