Solved

How to get text before a a certain character

Posted on 2014-01-13
4
367 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

695 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