How to break a string at each comma?

前端 未结 6 1873
再見小時候
再見小時候 2021-01-24 15:11

Hi guys I have a problem at hand that I can\'t seem to figure out, I have a string (C#) which looks like this:

string tags = \"cars, motor, whee         


        
6条回答
  •  迷失自我
    2021-01-24 15:51

    You are looking for the C# split() function.

    string[] tags = tags.Split(',');
    

    Edit:

    string[] tag = tags.Trim().Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries);
    

    You should definitely use the form supplied by Justin Niessner. There were two key differences that may be helpful depending on the input you receive:

    1. You had spaces after your ,s so it would be best to split on ", "

    2. StringSplitOptions.RemoveEmptyEntries will remove the empty entry that is possible in the case that you have a trailing comma.

提交回复
热议问题