Solved

remove part of string

Posted on 2009-04-03
4
314 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 125 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 125 total points
ID: 24059109
string x = "skj(ælsu(39)";
if(x.Contains('('))
       x=x.Substring(0,x.LastIndexOf('(');
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

733 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