Solved

String Manipulation???

Posted on 2008-10-15
14
414 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
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
 
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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
cloud web Service looking for a home... 3 106
Problem to go to page 12 95
Configure a Local PHP Interpreter for Phpstorm 2 262
UPLOAD FILE TO Web API USING POST 5 117
This is a guide to setting up a new WHM/cPanel Server to be used for web hosting accounts. It is intended for web hosting company administrators and dedicated server owners. For under $99 per month (considering normal rate of Big Data Cetnters like …
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

831 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