Solved

remove part of string

Posted on 2009-04-03
4
316 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

Automating Terraform w Jenkins & AWS CodeCommit

How to configure Jenkins and CodeCommit to allow users to easily create and destroy infrastructure using Terraform code.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

624 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