Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

String Manipulation???

Posted on 2008-10-15
14
Medium Priority
?
424 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Looking for a new Web Host?

Lunarpages' assortment of hosting products and solutions ensure a perfect fit for anyone looking to get their vision or products to market. Our award winning customer support and 30-day money back guarantee show the pride we take in being the industry's premier MSP.

 
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 1200 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 400 total points
ID: 22721202
oops you don't even need the evaluate

<cfset strOut = replace(str, "_"  & listlast(str,"_"),"")>
0
 
LVL 8

Assisted Solution

by:eszaq
eszaq earned 400 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

Lease-to-own eliminates the expenditure of hardware replacement and allows you to pay off the server over time. Usually, this is much cheaper than leasing servers. Think of lease-to-own as credit without interest.
What You Need to Know when Searching for a Webhost Provider
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

618 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