Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
left(yourstring,1)
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
<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>
Copy link to clipboard
Copied
thanks for the replies.
I ended going with <cfif #therow.startsWith("[")#>
Copy link to clipboard
Copied
Better: <cfif therow.startsWith("[")>
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.