Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

remove part of string

Posted on 2009-04-03
4
Medium Priority
?
321 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
  • 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

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

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

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…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

783 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