Solved

remove part of string

Posted on 2009-04-03
4
309 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
Comment Utility
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
Comment Utility
.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
Comment Utility
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
Comment Utility
string x = "skj(ælsu(39)";
if(x.Contains('('))
       x=x.Substring(0,x.LastIndexOf('(');
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now