?
Solved

remove part of string

Posted on 2009-04-03
4
Medium Priority
?
317 Views
Last Modified: 2013-12-17
I have a string. I want to find the last instance of "(" and remove it and everything that is behind it.
For example:"skj(ælsu(39)".  I would like to return: "skj(ælsu"
How do I do that
0
Comment
Question by:johnkainn
[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
  • 2
  • 2
4 Comments
 
LVL 23

Expert Comment

by:adathelad
ID: 24058796
Hi,

This should do the trick:
string val = "skj(ælsu(39)";
int lastPosition = val.LastIndexOf('(');
if (lastPosition>0)
    val = val.Substring(0, lastPosition);

Open in new window

0
 
LVL 23

Accepted Solution

by:
adathelad earned 500 total points
ID: 24058811
.LastIndexOf finds the last position of the specified character in the given string variable.
You have to check this before using in Substring as if there are no instances of the character in the string, it returns -1 which is an invalid argument to Substring
0
 
LVL 6

Expert Comment

by:SaedSalman
ID: 24059089
string x = "skj(ælsu(39)";
if(x.Contains('(')
x=x.Substring(0,x.LastIndexOf('(');
0
 
LVL 6

Assisted Solution

by:SaedSalman
SaedSalman earned 500 total points
ID: 24059109
string x = "skj(ælsu(39)";
if(x.Contains('('))
       x=x.Substring(0,x.LastIndexOf('(');
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

718 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