Aliaspooryorik
ColdFusion ORM Book

Show the first n and last n words

Ben Nadel, blogged Ask Ben: Displaying A Blog Teaser (Showing The First N Words) yesterday, where he showed how to get the first 50 words from an article using regular expressions. I posted in the comments an alternative way to do it (I'm not saying it's better!) using the Java Util Array class and thought I'd blog it here as well.


<cfsavecontent variable="blogContent">
I am just writing some random words here so that I can demonstrate using the java.util.Arrays class
in ColdFusion. As you know ColdFusion runs on top of java which means we can use it in our code. One of
my favourites is the addAll method to merge two arrays, but we'll save that for another day! OK, that
should be enough text to work with.
</cfsavecontent>

<!--- create a Java Arrays object --->
<cfset javaArrayObject = CreateObject( "java","java.util.Arrays" ) />
<!--- convert to array using a space as a delimiter and get first 10 elements --->
<cfset wordArray = javaArrayObject.copyOf( blogContent.Split( " " ), 10 ) />
<!--- convert back to a string --->
<cfset blogSnippet = ArrayToList( wordArray, " " ) />

<p>#blogSnippet#</p>

For anyone that's interested I'll break it down a bit more to explain what is going on.


<!--- create a Java Array object (not the same as a ColdFusion array) --->
<cfset JavaArrayObject = CreateObject( "java","java.util.Arrays" ) />
<!--- convert to a java array using a space as a delimiter --->
<cfset arrayOfWords = blogContent.Split( " " ) />
<!--- copy the first 50 elements in the array --->
<cfset wordArray = JavaArrayObject.copyOf( arrayOfWords, 10 ) />
<!--- convert back to a string --->
<cfset blogSnippet = ArrayToList( wordArray, " " ) />

<p>#blogSnippet#</p>

As you can see, we can use the copyOf method to get the first 10 elements in an array. The java.utl.Array class has another trick up it's sleeve and that is the copyOfRange method, which allows us to select elements x to y of an array. This means we can do something like this to get the first and last 10 words of a block of text.


<cfset JavaArrayObject = CreateObject( "java", "java.util.Arrays" ) />
<cfset articleArray = blogContent.Split( " " ) />
<cfset startWordArray = JavaArrayObject.copyOf( articleArray, 10 ) />
<cfset endWordArray = JavaArrayObject.copyOfRange( articleArray, ArrayLen( articleArray ) - 10, ArrayLen( articleArray ) ) />
<cfset blogSnippet = ArrayToList( startWordArray, " " ) & "&hellip;<br />&hellip;" & ArrayToList( endWordArray, " " ) />    
    
<p>#blogSnippet#</p>

Neat huh!

 


1 comment

  1. Thanks for posting I need something like this right now.

    Comment by James White – February 23, 2010

Leave a comment

If you found this post useful, interesting or just plain wrong, let me know - I like feedback :)

Please note: If you haven't commented before, then your comments will be moderated before they are displayed.

Please subscribe me to any further comments
 

Search

Wish List

Found something helpful & want to say ’thanks‘? Then visit my Amazon Wish List :)

Categories

Recent Posts