c# - How to break a string at each comma? -


hi guys have problem @ hand can't seem figure out, have string (c#) looks this:

string tags = "cars, motor, wheels, parts, windshield"; 

i need break string @ every comma , each word assign new string like:

string individual_tag = "car"; 

i know have kind of loop here i'm not sure how approach this, appreciate it.

you can use 1 of string.split methods

split method (char[]) split method (char[], stringsplitoptions) split method (string[], stringsplitoptions) 

let's try second option: i'm giving , , space split chars on each character occurrence input string split, there can empty strings in results. can remove them using stringsplitoptions.removeemptyentries parameter.

string[] tagarray = tags.split(new char[]{',', ' '},                                stringsplitoptions.removeemptyentries); 

or

 string[] tagarray = s.split(", ".tochararray(),                                 stringsplitoptions.removeemptyentries); 

you can access each tag by:

foreach (var t in tagarray ) {     lbltags.text = lbltags.text + " " + t; // update lable tag values      //system.diagnostics.debug.writeline(t); // result can see on vs out put window  } 

Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -