Solved

String Manipulation???

Posted on 2008-10-15
14
412 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
Comment Utility
try this
#REReplace(name,"_[0-9]*","")#

Open in new window

0
 
LVL 1

Author Comment

by:Westside2004
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
oops you don't even need the evaluate

<cfset strOut = replace(str, "_"  & listlast(str,"_"),"")>
0
Superior storage. Superior surveillance.

WD Purple drives are built for 24/7, always-on, high-definition security systems. With support for up to 8 hard drives and 32 cameras, WD Purple drives are optimized for surveillance.

 
LVL 8

Assisted Solution

by:eszaq
eszaq earned 100 total points
Comment Utility
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
Comment Utility
Same effect using list functions:
#ListDeleteAt("last_name_2267", ListLen("last_name_2267", "_"), "_")#
0
 
LVL 63

Expert Comment

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

Author Comment

by:Westside2004
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 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

12 Experts available now in Live!

Get 1:1 Help Now