Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

First character of a string?

Guest
Sep 07, 2009 Sep 07, 2009

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!

3.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 07, 2009 Sep 07, 2009

left(yourstring,1)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 07, 2009 Sep 07, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 07, 2009 Sep 07, 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>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Sep 10, 2009 Sep 10, 2009

thanks for the replies.

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 10, 2009 Sep 10, 2009

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 10, 2009 Sep 10, 2009

thanks for the replies.

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

I think this is fine, other than - as BKBK suggests - you don't need the pound-signs there.

However as a general rule of thumb, I don't mix languages if I don't need to.  CF provided the construct for this: left(therow, 1), so I don't see the merit in using a Java method call to effect the same thing.

You might be au fait with the ins and outs of Java string methods, but the next person who comes along to maintain this code might not be.

--

Adam

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 11, 2009 Sep 11, 2009

However as a general rule of thumb, I don't mix languages if I don't need to.  CF provided the construct for this: left(therow, 1), so I don't see the merit in using a Java method call to effect the same thing.

You might be au fait with the ins and outs of Java string methods, but the next person who comes along to maintain this code might not be.

The startsWith was just to resonate with Craig_uk's mention of beginsWith and his request for ideas. However, there is some wisdom is what you say.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 11, 2009 Sep 11, 2009

Ah.  I didn't notice that the startsWith() thing was from your suggestion.  I think it's good to suggest these things so people are aware that they are possibilities.  Indeed I didn't know about that method until I spotted it being mentioned here.  And hopefully other readers might go have a look at what else is on offer with the java.lang.String class after reading this thread too.  All good.

On looking more closely @ your code now, just another thing: there's no need to specifically create a Java string the way you do there: CF strings are already Java strings.  And of course CF lists (or list elements in this case) are - likewise - just strings.

One can just do this (for example):

<cfset s = "foo">
<cfoutput>#s.startsWith("f")#</cfoutput>

Indeed this is perhaps a better example:

<cfoutput>#s.getClass().getName()#</cfoutput>

--

Adam

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 12, 2009 Sep 12, 2009
LATEST

Indeed this is perhaps a better example:

<cfoutput>#s.getClass().getName()#</cfoutput>

No need.

On looking more closely @ your code now, just another thing: there's no need to specifically create a Java string the way you do there: CF strings are already Java strings.  And of course CF lists (or list elements in this case) are - likewise - just strings.

One can just do this (for example):

<cfset s = "foo">
<cfoutput>#s.startsWith("f")#</cfoutput>

Naturally. Sometimes one continues discharging the bazooka when there's no longer the enemy chinook, but a mosquito.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources