Skip to main content
September 7, 2009
Question

First character of a string?

  • September 7, 2009
  • 3 replies
  • 3349 views

Hi,

I am looping through a fixed width text file and importing the data into a database.

I have one problem, some of the lines of data beging with a "[" character, i would like to get CF to ignore these lines of data.

I have looked for a Beginswith function or something similar but cannot find anything.

Ideas? Thanks in advance!

    This topic has been closed for replies.

    3 replies

    BKBK
    Community Expert
    September 7, 2009

    <cfsavecontent variable="x">
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    []ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss
    dddddddddddddddddddddddddddddddddddddddddddddd
    vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb]
    fffffffffffffff
    gggggggggggg
    hhhhhhhhhhhhhhh
    </cfsavecontent>

    <cfloop list="#x#" delimiters="#chr(13)##chr(10)#" index="listElement">
        <cfset currentLine=createobject("java","java.lang.String").init(listElement)>
        <cfif NOT currentLine.startsWith("[")>
            <cfoutput>#currentLine#</cfoutput><br>
        </cfif>   
    </cfloop>

    September 10, 2009

    thanks for the replies.

    I ended going with <cfif #therow.startsWith("[")#>

    BKBK
    Community Expert
    September 10, 2009

    Better: <cfif therow.startsWith("[")>

    Inspiring
    September 7, 2009

    Do you want to identify the first character, or do you want to remove it if it's a "["?  Those are two different things.

    Dan's answered the former; the latter can be done with a regular expression replacement:

    reReplace(s, "^\[?(.*)", "\1", "ONE")

    It's probably a good idea to read-up the regex docs to understand why that's the solution:

    http://livedocs.adobe.com/coldfusion/8/htmldocs/regexp_01.html#1100400

    Regular expressions are one of the most powerful capabilities CF has regarding string manipulation, so it's a good skill to have to be able to use them.

    --

    Adam

    Inspiring
    September 7, 2009

    left(yourstring,1)