Solved

Coldfusion How can I trim my output to not show the first 11 or 7 characters?

Posted on 2014-03-26
4
307 Views
Last Modified: 2014-03-26
My search results look like this:

Mainserver\Spongebob

How can I trim the first 11 characters and have the output look like:

Spongebob

I'm using Replace:
Replace(updateby,"Mainserver\","")

Problem: Some entries have "Mainserver\" or "Server\"
How can I replace if "Mainserver\" or "Server\"?
0
Comment
Question by:DJPr0
  • 2
  • 2
4 Comments
 
LVL 52

Expert Comment

by:_agx_
ID: 39956887
If the value always contains a "\", try using list functions instead. Does it always have 2 parts ie Like "Mainserver" and "Spongebob" ? If so you could use listLast:

     ListLast("Mainserver\Spongebob", "\")

EDIT:  Otherwise, try getToken():

     <!--- get the 2nd piece of "\" delimited string ie "SecondElement"
           "FirstElement\SecondElement\ThirdElement...."

            If the list only has one element, this will return an empty string ""
     --->
     getToken("Mainserver\Spongebob", 2, "\")
0
 

Author Comment

by:DJPr0
ID: 39957032
Not all values have 2 parts, could be:

Mainserver\Spongebob
Server\Spongebob
Spongebob

Can we have 2 Replace statements or:

Replace(updateby,("Mainserver\"or"Server"),"")
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 39957080
If you always want to omit the 1st part, just do an if/else:

      <cfset str = "Mainserver\SpongeBob">
      <cfif listLen(str, "\") lte 1>
           <!--- show string as is --->
            #str#
      <cfelse>
           <!--- remove 1st element, whatever it is --->
            #listRest(str, "\")#
      </cfif>

If you want to do a plain replace, you'll need to call the function twice. If the case can vary, use the case insensitive version:

          ReplaceNoCase(ReplaceNoCase(updateBy, "MainServer", ""), "Server", "")
0
 

Author Closing Comment

by:DJPr0
ID: 39957423
Thanks _agx_!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

685 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