[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
SolvedPrivate

How to find the last character in string and replace it

Posted on 2014-09-02
11
Medium Priority
?
52 Views
Last Modified: 2016-02-17
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

0
Comment
Question by:Camillia
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 9

Assisted Solution

by:s_chilkury
s_chilkury earned 668 total points
ID: 40298865
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
 
LVL 7

Author Comment

by:Camillia
ID: 40298868
There has to be a way to do it in LINQ
0
 
LVL 7

Author Comment

by:Camillia
ID: 40298878
and what is that "foo"??
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 9

Expert Comment

by:s_chilkury
ID: 40298901
replace "foo" with ")" - thats the character you want to replace
0
 
LVL 7

Author Comment

by:Camillia
ID: 40298904
There's no way to do this with LINQ??
0
 
LVL 9

Expert Comment

by:s_chilkury
ID: 40298938
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
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40299022
Hi Camilla;

How about just trimming off the last ) from the string

filterString = filterString.TrimEnd(')');

Open in new window

0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 664 total points
ID: 40299529
@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
 
LVL 7

Author Comment

by:Camillia
ID: 40299559
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
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 668 total points
ID: 40300108
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
 
LVL 7

Author Comment

by:Camillia
ID: 40300114
Let me try it. I'll post back.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month20 days, 12 hours left to enroll

865 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