Search code examples
coldfusioncoldfusion-9

How to remove words or chars from a string using coldfusion


I am new to coldfusion and my goal is to remove part of a string according to certain words.

For example:

<cfset myVar = "One of the myths associated with the Great Wall of China is that it is the only man-made structure"/>¨

How can I remove the words "One of the myths associated with the" in order to have
Great Wall of China is that it is the only man-made structure as string?

I used following function

RemoveChars(string, start, count)

But I need to create a function maybe with RegEx or native coldfusion functions.


Solution

  • You could see the sentence as a list seperated by spaces. So if you want to cut off your sentence to start with "Great Wall of China", you could try

    <cfloop list="#myVar#" index="word" delimiters=" ">
      <cfif word neq "Great">
         <cfset myVar = listRest(#myVar#," ")>
      <cfelse>
         <cfbreak>
      </cfif>
    </cfloop>
    <cfoutput>#myVar#</cfoutput>
    

    There may be a quicker way to do this. Here's a function at cfLib.org that can alter a list in a similar way: LINK.