How to get text before a a certain character

Hi there

Hope you can help me?

I have a string like this

string name = "Fred Blog (Feb 2009)";

Can someone please write a method that will get me ALL the text BEFORE the '('

i.e.

Fred Blog

thanks
mousemat24Asked:
Who is Participating?
 
Angelp1ayConnect With a Mentor Commented:
If you need to handle cases where the ( doesn't exist you need to test the return of IndexOf e.g:
int bracketPos = name.IndexOf('(');
If(!bracketPos.Equals(-1)) {
    name = name.SubString(0,bracketPos);
}

Open in new window


If you want to strip leading/trailing spaces use .Trim() as well:
name = name.Trim();

Open in new window

0
 
Angelp1ayCommented:
name.SubString(0, name.IndexOf('('));

Open in new window

0
 
mousemat24Author Commented:
thanks man, that's what I wanted
0
 
Angelp1ayCommented:
You're welcome, have a nice day :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.