Solved

How to get text before a a certain character

Posted on 2014-01-13
4
354 Views
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 '('

i.e.

Fred Blog

thanks
0
Comment
Question by:mousemat24
  • 3
4 Comments
 
LVL 11

Expert Comment

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

Open in new window

0
 
LVL 11

Accepted Solution

by:
Angelp1ay earned 500 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

0
 

Author Comment

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

Expert Comment

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

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

770 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