Solved

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

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Email Header Detail 12 63
SQL Server group with two values 4 34
SQL query and VBA 5 41
reccommendations for a free msft sql query manager? 4 25
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
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…

790 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