How to get text before a a certain character

Posted on 2014-01-13
Medium Priority
Last Modified: 2014-01-13
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 '('


Fred Blog

Question by:mousemat24
  • 3
LVL 11

Expert Comment

ID: 39777042
name.SubString(0, name.IndexOf('('));

Open in new window

LVL 11

Accepted Solution

Angelp1ay earned 2000 total points
ID: 39777055
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


Author Comment

ID: 39777060
thanks man, that's what I wanted
LVL 11

Expert Comment

ID: 39777102
You're welcome, have a nice day :)

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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.

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Watch the video of Kernel Migrator for SharePoint, which demonstrate the process easily of migration from SharePoint to SharePoint, OneDrive for Business & Google Drive servers, Public Folder to SharePoint, File Server to SharePoint. The tool has va…

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question