Alan Alan - 18 days ago 7
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
cat
dog


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

house
cat
dog


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
string str = "house; home; residence";
string firstWord = str.Split(';').FirstOrDefault();