Lasso Soft Inc. > Home

[string_truncate2]

Linkstring_truncate2
AuthorJason Huck
CategoryString
Version8.5.x
LicensePublic Domain
Posted04 Jan 2008
Updated04 Jan 2008
More by this author...

Description

This tag accepts a string and a number of characters, and returns the string truncated to that length. It makes allowances for whitespace and punctuation, and appends an ellipsis character to the end. A complete description can be found here.

Sample Usage

var('str') = 'The quick, brown-fox jumps over the "lazy" dog.'; 
 
loop($str->size); 
    loop_count + ' - ' + string_truncate($str, loop_count) + '\n'; 
/loop; 

Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.

define_tag(
	'truncate',
	-namespace='string_',
	-req='text',
	-req='length', -type='integer', -copy,
	-priority='replace',
	-encodenone,
	-description='Truncates the given string to the given number of characters.'
);
	// if the original string is shorter than or equal to the desired length,
	// just return it unaltered.
	#text->size <= #length ? return(#text);
	
	local('out') = string;
	
	// while #out is empty, #length is still greater than zero,
	// and the last character of the new string is not whitespace...
	while(!#out->size || !#out->iswhitespace(#out->size) && #length);
		// store a new substring in #out
		#out = #text->substring(1, #length);
		// decrement #length by 1
		#length -= 1;
	/while;
	
	// if we reached zero, return nothing
	!#length ? return;
			
	// remove any trailing non-alphanumeric characters and whitespace
	#out = string_replaceregexp(
		#out,
		-find='[^A-Za-z0-9]*\\s*$',
		-replace=''
	);
	
	// return the final result with an ellipsis character appended
	return(#out + '…');	
/define_tag;

Related Tags

Comments

No comments

Please log in to comment

LassoSoft Inc. > Home

 

 

©LassoSoft Inc 2014 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft