Skip to main content
Inspiring
July 31, 2007
Answered

Extract from String

  • July 31, 2007
  • 4 replies
  • 737 views
I have strings that are file names...like index.cfm......test.pdf....messagepost.cfm

I need to extract only what is to the left of the dot.

Like for index.cfm...I need to extract index

for test.pdf..I need to extract test

for messagepost.cfm .. I need to extract messagepost

I've tried variations of right ,left, mid..to no avail..

Any help.
    This topic has been closed for replies.
    Correct answer cf_dev2
    <cfset yourString = "mesagepost.cfm">
    <cfset index = find(".", reverse(yourString))>
    <cfset fileName = left(yourString, len(yourString)-index)>
    <cfoutput>fileName=#fileName#</cfoutput>

    4 replies

    Inspiring
    July 31, 2007
    I tend to use the reverse()/find() approach for file names because it handles odd cases like multiple file extensions: myFile .xls.txt. But if you don't have to handle special cases, then use list functions with a "." as the delimiter.
    cf_dev2Correct answer
    Inspiring
    July 31, 2007
    <cfset yourString = "mesagepost.cfm">
    <cfset index = find(".", reverse(yourString))>
    <cfset fileName = left(yourString, len(yourString)-index)>
    <cfoutput>fileName=#fileName#</cfoutput>
    Fernis
    Inspiring
    August 1, 2007
    I find this the most logical way for me. It handles multiple file extensions as well, removing only the last one.

    <cfset yourString = "messagepost.cfm">
    <cfset fileName = listDeleteAt(yourString,listLen(yourString,"."),".")>
    <cfoutput>#fileName#</cfoutput>

    -Fernis
    Inspiring
    August 1, 2007
    Good idea. But I'm still sticking with reverse()/find() because it handles the "no file extension" case.
    Participating Frequently
    July 31, 2007
    You can use getToken() or listFirst() functions. I'll leave it up to you to research the functions.

    T
    Inspiring
    July 31, 2007
    think of your filenames as dot-delimited lists - use listfirst() function

    ---
    Azadi Saryev
    Sabai-dee.com
    http://www.sabai-dee.com