Solved

How to get text before a a certain character

Posted on 2014-01-13
4
359 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.Net creating Contact in Outlook 1 60
c# DateTime Format validation 4 70
Adjust the codes 3 38
aspx ascx, c# 7 21
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

697 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