Solved

String Manipulation???

Posted on 2008-10-15
14
415 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
Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

 
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

Flexible connectivity for any environment

The KE6900 series can extend and deploy computers with high definition displays across multiple stations in a variety of applications that suit any environment. Expand computer use to stations across multiple rooms with dynamic access.

Question has a verified solution.

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

Suggested Solutions

When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

828 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