Solved

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

Posted on 2014-03-26
4
301 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 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

19 Experts available now in Live!

Get 1:1 Help Now