Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 425
  • Last Modified:

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
0
Westside2004
Asked:
Westside2004
  • 4
  • 3
  • 2
  • +3
3 Solutions
 
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
 
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Open in new window

0
 
ZvonkoSystems 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
 
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
 
SidFishesCommented:
oops you don't even need the evaluate

<cfset strOut = replace(str, "_"  & listlast(str,"_"),"")>
0
 
eszaqCommented:
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now