• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 387
  • Last Modified:

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
0
mousemat24
Asked:
mousemat24
  • 3
1 Solution
 
Angelp1ayCommented:
name.SubString(0, name.IndexOf('('));

Open in new window

0
 
Angelp1ayCommented:
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
 
mousemat24Author Commented:
thanks man, that's what I wanted
0
 
Angelp1ayCommented:
You're welcome, have a nice day :)
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now