Lasso Soft Inc. > Home

[lp_string_split]

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

Description

Similar to ->split except -limit=n limits split to just n elements.

Sample Usage

lp_string_split:'hello?1?2?3?','?',3; = [lp_string_split:'hello?1?2?3?','?',3]
lp_string_split:'hello?1?2?3?','?'; = [lp_string_split:'hello?1?2?3?','?']
lp_string_split:'12345','',2; = [lp_string_split:'12345','',2]
lp_string_split:'12345',''; = [lp_string_split:'12345','']

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_split',
	-description='Similar to ->split except -limit=n limits split to just n elements.',
	-priority='replace',
	-required='string', -copy,
	-required='delimiter', -copy,
	-optional='limit', -copy;

	#string = string:#string;
	#delimiter = string:#delimiter;

	local:'split' = #string->(split:#delimiter);
	if: !(local_defined:'limit');
		return: #split;
	else;
		#limit = integer: #limit;
		local:'split_size' = #split->size;
		if: #limit < 2;
			return: (array: #string);
		else: #limit >= #split_size;
			return: #split;
		else; // split is more than the limit allows

			local:'newsplit' = array;
			loop: #split_size;
				if: loop_count < #limit;
					#newsplit->(insert: (#split->(get:1)));
					#split->(remove:1);
				else;
					#newsplit->(insert: (#split->(join:#delimiter)));
					return: #newsplit;
				/if;				
			/loop;
		/if;
	/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