Aliaspooryorik
ColdFusion ORM Book

Remove the last occurrence of a character in a string

Just a quick post in response to a question my collegue Simon Bingham, has just asked me. He has a string, and needs to remove the last occurrence of a specific character from it. There are several solutions (some involving loops!), however by taking advantage of the Java String methods we can do it quite easily.

Here's a quick example of 3 different ways to accomplish the same thing.

<!--- original string that we want to remove the last comma from --->
<cfset mystring = "The cat, in the hat, sat on the mat, fancy that!" />
<cfoutput>
<!--- using ReReplace function with a backreference (thanks Paul Klinkenberg) --->
#ReReplace(mystring, '(.*),', '\1')#<br />
<!--- using CFML Reverse function and the replaceFirst Java String method --->
#Reverse(Reverse(mystring).replaceFirst(',',''))#<br />
<!--- using CFML RemoveChars function and the lastIndexOf Java String method --->
#RemoveChars(mystring,mystring.lastIndexOf(',')+1,1)#<br />
</cfoutput>

All of the above techniques will return:

The cat, in the hat, sat on the mat fancy that!

Of course, now he's changed his mind and wants to replace it, so with a quick mod we can get the desired result.

<!--- original string that we want to replace the last comma from with an &amp; --->
<cfset mystring = "The cat, in the hat, sat on the mat, fancy that!" />
<cfoutput>
<!--- using ReReplace function with a backreference (thanks Paul Klinkenberg) --->
#ReReplace(mystring, '(.*),', '\1 &amp;')#<br />
<!--- using CFML Reverse function and the replaceFirst Java String method --->
#Reverse(Reverse(mystring).replaceFirst(',',';pma& '))#
</cfoutput>

We now get:

The cat, in the hat, sat on the mat & fancy that!

 


8 comments

  1. Thanks John... :o)

    Comment by Simon Bingham – June 03, 2009
  2. Hi John,

    My code is now doing what I want it to do.

    <cfset foo=REReplace( foo, "[^..]", "" ) />
    <cfset foo=Reverse( Reverse( foo ).replaceFirst( ',', ' ;pma& ' ) ) />

    The code converts a list from (for example):

    ", Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday"

    to:

    "Monday, Tuesday, Wednesday, Thursday, Friday, Saturday & Sunday"

    Very handy!

    Comment by Simon Bingham – June 03, 2009
  3. Glad to help :) I think you want to change the your Regular Expression to:
    ReReplaceNoCase( mystring, "^, ", "" )

    Comment by John Whish – June 03, 2009
  4. Remving the last occurence of a character in a string is as easy as: <cfset newStr = rereplace(str, '(.*)#charToReplace#', '\1') />

    Because regEx is greedy, it takes as much chars as possible into .* before matching the (last occurence of the) character.

    Comment by Paul Klinkenberg – June 03, 2009
  5. Hi Paul, that's very cool. I hadn't thought about using a back reference. I'll update my post. Thanks!

    Comment by John Whish – June 04, 2009
  6. Of course. This is just what I was looking for. Thank you for the articl :]

    Comment by Ryan v. – July 22, 2009
  7. This is wonderful!!!
    This made my work in minutes

    Thanks!

    Comment by sureen – November 02, 2009
  8. I've been working on a client project and really needed this! Thank you so much!

    Comment by Michael B. – February 16, 2010

Leave a comment

If you found this post useful, interesting or just plain wrong, let me know - I like feedback :)

Please note: If you haven't commented before, then your comments will be moderated before they are displayed.

Please subscribe me to any further comments
 

Search

Wish List

Found something helpful & want to say ’thanks‘? Then visit my Amazon Wish List :)

Categories

Recent Posts