Okay, I get that performing the following will create an array of my string split on the _
:
<cfset theString = "get_the_point">
<cfset thePieces = theString.Split("_{1}")>
But, how do I get just the 3rd item in the array without having to create a new variable that evaluates the array like:
<cfset theThirdPiece = thePieces[3]>
Is there something to the .Split()
that I don't know about that can return just the specific item I'm looking for? This has to be super easy because I didn't find anything in the documentati开发者_如何学Con. Or I was searching it wrong.
If you are using a simple delimiter like the underscore, you can do:
<cfset thePiece = listGetAt("get_the_point", 3, "_")>
All list functions take an optional delimiter argument that is a list of single characters to use as delimiters.
Remember to check the listLen() of the list before attempting to access the nth location.
Just to complete the whole picture I would propose a bit simpler/cleaner way to do what you need -- if you need the last
item:
<cfset thePiece = ListLast("get_the_point", "_") />
BTW, looking into the Java-related discussion in the comments of accepter answer I would note that there's a native function for splitting:
<cfset theString = "get_the_point" />
<cfset thePieces = ListToArray(theString, "_") />
I suppose it uses the same Split
under the hood.
Just to clarify that it is syntactically correct I should point out that you could also do this:
<cfset lastOne = theString.Split("_")[3] />
I believe this only works in CF9+
精彩评论