String Manipulation???

Hi,

How can I strip off some characters from the end of a string?  I am not very good at regular expressions but perhaps I may not need one?  Here is the data

first_name_510
last_name_2267

I need a function that will strip off everything from the right including the underscore.  I should be left with the below:

first_name
last_name

Any help highly appreciated

-ws
LVL 1
Westside2004Asked:
Who is Participating?
 
ZvonkoConnect With a Mentor Systems architectCommented:
Here my proposal:
<cfset fname="first_name_510" >
<cfset lname="last_name_2267" >
 
 
<cfset fname=rereplace(fname,"_\d+$","") >
<cfset lname=rereplace(lname,"_\d+$","") >
 
<cfoutput>
#fname# #lname#
</cfoutput>

Open in new window

0
 
HuyBDCommented:
try this
#REReplace(name,"_[0-9]*","")#

Open in new window

0
 
Westside2004Author Commented:
Hi,

Close somewhat, it left me with:

FirstName_5

As opposed to just: First_Name

It looks like it removed the first underscore as opposed stripping everything from the right up to and including the underscore

To reiterate:

First_Name_523

would become

First_Name

Any help appreciated :)

-ws

0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
GeorgCommented:
Hello,

try this:

<cfset str1="first_name_510">
<cfset str2="last_name_2267">

<cfset newstr1="#Left(str1, (Len(str1)-( Find("_", Reverse(str1), 1) )))#">
<cfset newstr2="#Left(str2, (Len(str2)-( Find("_", Reverse(str2), 1) )))#">

the output of newstr1 and newstr2 is:
first_name
last_name

Georg

0
 
HuyBDCommented:
sorry
<cfset name=REReplace(name,"[0-9]*","","ALL")>
<cfset name=left(name,len(name)-1)>

Open in new window

0
 
SidFishesCommented:
an alternative in a single line using list functions

<cfset str = "first_name_510">

<cfset strOut = replace(str, "_"  & evaluate(listlast(str,"_")),"")>


<cfoutput>#strOut#</cfoutput>
0
 
SidFishesConnect With a Mentor Commented:
oops you don't even need the evaluate

<cfset strOut = replace(str, "_"  & listlast(str,"_"),"")>
0
 
eszaqConnect With a Mentor Commented:
You should use regular expressions. ReReplace() function, not Replace(). And make sure you address end of string with dollar sign:
<cfset fname=rereplace(fname,"_\d+$","")>
Do the same for last name.
0
 
eszaqCommented:
Same effect using list functions:
#ListDeleteAt("last_name_2267", ListLen("last_name_2267", "_"), "_")#
0
 
ZvonkoSystems architectCommented:
@eszaq: "You should use regular expressions."
You should read the thread before posting: http:#22719380
0
 
Westside2004Author Commented:
Thanks for all the responses here.  I will test this out tonight and post back shortly.

Thanks,

-ws
0
 
Westside2004Author Commented:
Great answers!.  Thanks, one thing, how can you remember the syntax for these regular expressions?  They seem powerful but how do you know which characters to use?
0
 
Westside2004Author Commented:
Thanks for the help! One thing though, how can you remember the syntax for these regular expressions?  They seem powerful but how do you know which characters to use?

-ws
0
 
ZvonkoSystems architectCommented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.