Solved

String Manipulation???

Posted on 2008-10-15
14
413 Views
Last Modified: 2013-12-24
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
Comment
Question by:Westside2004
  • 4
  • 3
  • 2
  • +3
14 Comments
 
LVL 17

Expert Comment

by:HuyBD
ID: 22718665
try this
#REReplace(name,"_[0-9]*","")#

Open in new window

0
 
LVL 1

Author Comment

by:Westside2004
ID: 22718776
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
 
LVL 1

Expert Comment

by:Georg
ID: 22718883
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
 
LVL 17

Expert Comment

by:HuyBD
ID: 22719149
sorry
<cfset name=REReplace(name,"[0-9]*","","ALL")>

<cfset name=left(name,len(name)-1)>

Open in new window

0
 
LVL 63

Accepted Solution

by:
Zvonko earned 300 total points
ID: 22719380
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
 
LVL 36

Expert Comment

by:SidFishes
ID: 22721172
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
 
LVL 36

Assisted Solution

by:SidFishes
SidFishes earned 100 total points
ID: 22721202
oops you don't even need the evaluate

<cfset strOut = replace(str, "_"  & listlast(str,"_"),"")>
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 8

Assisted Solution

by:eszaq
eszaq earned 100 total points
ID: 22736609
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
 
LVL 8

Expert Comment

by:eszaq
ID: 22736636
Same effect using list functions:
#ListDeleteAt("last_name_2267", ListLen("last_name_2267", "_"), "_")#
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 22738229
@eszaq: "You should use regular expressions."
You should read the thread before posting: http:#22719380
0
 
LVL 1

Author Comment

by:Westside2004
ID: 22751048
Thanks for all the responses here.  I will test this out tonight and post back shortly.

Thanks,

-ws
0
 
LVL 1

Author Closing Comment

by:Westside2004
ID: 31506208
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
 
LVL 1

Author Comment

by:Westside2004
ID: 22751094
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
 
LVL 63

Expert Comment

by:Zvonko
ID: 22751380
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In our day to day coding, how many times have we come across a necessity to check whether a URL is a broken link or not? For those of you that answered countless and are using ColdFusion like myself, then this article is for you.  It will show yo…
Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now