Question: Cutting a string in ColdFusion

Question

Cutting a string in ColdFusion

Answers 1
Added at 2016-12-19 23:12
Tags
Question

I have values in my query that looks like the following: Decrease with an active address (2) or Unsecured task (100) etc.

The value within the parentheses varies, it can be one, two, three digits or more because this is a count value.

I just need to get the description not the parentheses nor the value. So what I need is just:

Decrease with an active address
Unsecured task

etc.

How can I get rid of the opening (, the numeric value and the closing )?

In ColdFusion 8?

Answers
nr: #1 dodano: 2016-12-20 18:12

As Dan mentioned in the comments, one option is using reReplace() with the appropriate expression to remove any text within parenthesis:

<cfscript>
    origText = "Decrease with an active address (2)";
    newText = reReplaceNoCase(origText, "\([^)]*\)", "", "all");
    writeDump( newText );
</cfscript>

Update:

As Alex mentioned in the comments, if you just want to "cut" the string, and grab the part before the parenthesis, try something like this:

<cfscript>
    origText = "Decrease with an active address (2) plus more text after the parenthesis";
    newText = reReplaceNoCase(origText, "\([0-9]*\).*$", "", "all");
    writeOutput("<br>"& newText );
</cfscript>
Source Show
◀ Wstecz