Link to home
Start Free TrialLog in
Avatar of Camillia
CamilliaFlag for United States of America

asked on

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

SOLUTION
Avatar of s_chilkury
s_chilkury
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Camillia

ASKER

There has to be a way to do it in LINQ
and what is that "foo"??
replace "foo" with ")" - thats the character you want to replace
There's no way to do this with LINQ??
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.
Hi Camilla;

How about just trimming off the last ) from the string

filterString = filterString.TrimEnd(')');

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks, I'll give both a try...Fernando's and s_chilkury.

I don't want to trim ALL parens....just the last one.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Let me try it. I'll post back.