Alan Alan - 9 months ago 47
C# Question

How can I display the first word of a series of words that ends with a semicolon?

My application gets phrases from a database. These strings can look like this:

house; home; residence

How can I format these strings so that the resulting output looks like:


In other words if there's a semicolon immediately following the first word I would like to just include from the start up to the end of the semicolon and if no semicolon then just include the first word.

Answer Source
string str = "house; home; residence";
string firstWord = str.Split(';').FirstOrDefault();