How to find the last character in string and replace it

I have a string like this

var filterString ="WHERE ( {  StrToMember( @BrandId0 )  }, {  StrToMember( @RegistrationTypeId0 )  } )";

I want to replace the last ")" with blank. I did this but it looks like it replaces the entire string with blank.


filterString = filterString.Last().ToString().Replace(")", "");

Open in new window

LVL 8
CamilliaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

s_chilkuryCommented:
var bracStr ="WHERE ( {  StrToMember( @BrandId0 )  }, {  StrToMember( @RegistrationTypeId0 )  } )";

int lastIndex = bracStr.LastIndexOf(")");
           string subString =
                bracStr.Substring(lastIndex, bracStr.Length - lastIndex);
           subString = subString.Replace(")", "");
           bracStr = brac.Remove(lastIndex, bracStr.Length - lastIndex);
           bracStr += subString;
0
CamilliaAuthor Commented:
There has to be a way to do it in LINQ
0
CamilliaAuthor Commented:
and what is that "foo"??
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

s_chilkuryCommented:
replace "foo" with ")" - thats the character you want to replace
0
CamilliaAuthor Commented:
There's no way to do this with LINQ??
0
s_chilkuryCommented:
Check this:

http://stackoverflow.com/questions/10607104/linq-query-for-string-replace
http://frightanic.wordpress.com/2007/06/08/regex-match-last-occurrence/

For most string operations, you would be better off (conciseness) if you use regular expressions rather than LINQ. Will get back to you on LINQ solution too.
0
Fernando SotoRetiredCommented:
Hi Camilla;

How about just trimming off the last ) from the string

filterString = filterString.TrimEnd(')');

Open in new window

0
käµfm³d 👽Commented:
@Fernando Soto

May not be of consequence here, but that code will trim all trailing parens from the string (up to the first non-paren), not just the last.

@Farzadw

Certainly there are ways to achieve this with LINQ, but I don't know that any such way would make the code more readable--which is quite important these days. Honestly, I think what s_chilkury demonstrates is probably the way to go. You can always wrap such code up into a function--heck, you could even make it an extension method!
0
CamilliaAuthor Commented:
Thanks, I'll give both a try...Fernando's and s_chilkury.

I don't want to trim ALL parens....just the last one.
0
Fernando SotoRetiredCommented:
Hi Camilla;

Then you can do this just to remove the last occurrence of a closing parentheses.

var indexOfLast = filterString.LastIndexOf(')');
filterString = filterString.Remove(indexOfLast, 1);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CamilliaAuthor Commented:
Let me try it. I'll post back.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.