Lasso Soft Inc. > Home

[lp_string_pad]

Linklp_string_pad
AuthorBil Corry
CategoryString
Version8.x
LicensePublic Domain
Posted05 Dec 2005
Updated05 Dec 2005
More by this author...

Description

Returns a padded string.

Defaults:
    Pad character = '0' (zero)
    Padding = left (aka Alignment = right)
    Trim whitespace = true
    Crop = no crop

Requires [lp_integer_even]

Sample Usage

lp_string_pad Examples

[var:'string' = '123']string = "[$string]"

-padleft  5 using 0 = "[lp_string_pad: $string,5]"
-padright 5 using 0 = "[lp_string_pad: $string,5,-padright]"
-padleft  5 using * = "[lp_string_pad: $string,5,-char='*']"
-padright 5 using * = "[lp_string_pad: $string,5,-char='*',-padright]"


[var:'string' = '123456789']string = "[$string]"

-nocrop      = "[lp_string_pad: $string,5]"
-cropleft  5 = "[lp_string_pad: $string,5,-cropleft]"
-cropright 5 = "[lp_string_pad: $string,5,-cropright]"


[var:'string' = '  12']string = "[$string]"

-padleft  6          = "[lp_string_pad: $string,6]"
-padright 6          = "[lp_string_pad: $string,6,-padright]"
-padleft  6  -notrim = "[lp_string_pad: $string,6,-notrim]"
-padright 6  -notrim = "[lp_string_pad: $string,6,-padright,-notrim]"

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:'lp_string_pad',
	-description='Returns a padded string.',
	-priority='replace',
	-required='string',
	-required='length',
	-optional='char';

	// get params 

	local:'str'=string:#string;
	local:'len'=integer:#length;

	local:'pad' = '0';
	if: local_defined:'char';
		#pad = (string:#char);	
	/if;

	local:'padalign'='left';
	if: params->(find:'-padright')->size > 0 || params->(find:'-alignleft')->size > 0;
		#padalign = 'right';
	else: params->(find:'-padcenterright')->size > 0 || params->(find:'-aligncenterleft')->size > 0;
		#padalign = 'centerright';
	else: params->(find:'-padcenterleft')->size > 0 || params->(find:'-aligncenterright')->size > 0;
		#padalign = 'centerleft';
	/if;


	local:'crop'='none';
	if: params->(find:'-cropleft')->size > 0;
		#crop='left';
	else: params->(find:'-cropright')->size > 0;
		#crop='right';
	/if;

	if: params->(find:'-notrim')->size == 0;
		// trim it
		#str->trim;
	/if;

	
// work the magic 

	// if the string len and the wanted len are equal, just return the string
	if: #str->size == #len;
		return:#str;
	else: #str->size > #len;
		if: #crop == 'right';
			return:#str->(substring:1,#len);
		else: #crop == 'left';
			return:#str->(substring:#str->size - #len + 1,#str->size);
		else;
			return:#str;
		/if;
	else; // #str->size < #len
		local:'padlen' = #len - #str->size;
		if: #padalign == 'left';
			#str = (#pad * #padlen) + #str;
		else: #padalign == 'right';
			#str += (#pad * #padlen);
		else: (lp_integer_even: #padlen);
			#str = (#pad * (math_floor: #padlen / 2)) + #str + (#pad * (math_floor: #padlen / 2));
		else: #padalign == 'centerright';
			#str = (#pad * (math_floor: #padlen / 2)) + #str + (#pad * (1 + (math_floor: #padlen / 2)));
		else: #padalign == 'centerleft';
			#str = (#pad * (1 + (math_floor: #padlen / 2))) + #str + (#pad * (math_floor: #padlen / 2));
		/if;
		return:#str;
	/if;
/define_tag;

]

Comments

No comments

Please log in to comment

Subscribe to the LassoTalk mail list

LassoSoft Inc. > Home

 

 

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