Jump to content
Sign in to follow this  
kronzky

String Library

Recommended Posts

The library includes the following functions:

  • toArray: Converts string to an array
  • Length: Returns length of string
  • Left: Returns leftmost characters of string
  • Right: Returns rightmost characters of string
  • Mid: Returns substring
  • InStr: Tests for presence of search string
  • Index: Returns the position of a search string
  • Replace: Replaces every sub-string occurrence
  • Upper: Converts string to uppercase
  • Lower: Converts string to lowercase
  • findFlag: Finds a specific string in a mixed array
  • getArg: Returns an argument value from a mixed array
  • Compare: Compares two strings
  • ArraySort: Sorts multi-dimensional arrays

The package also contains a demo mission with multiple examples for each function.

Everything is available here.

Edited by Kronzky

Share this post


Link to post
Share on other sites
Cool stuff! Using the maximum number of characters ... brilliant! thumbs-up.gifnotworthy.gif

That was Kegetys' brilliant idea.

Just for the record... wink_o.gif

Share this post


Link to post
Share on other sites

Lol, I overlooked this thread. But I implemented the lib instantly and I noticed that no "_" char is in the _chars array. Perhaps worth to add?

Share this post


Link to post
Share on other sites
Lol, I overlooked this thread. But I implemented the lib instantly and I noticed that no "_" char is in the _chars array. Perhaps worth to add?

Done!

But with 16 votes already on the feature request I'm hoping that we won't have to rely on this crutch for much longer... wink_o.gif

Share this post


Link to post
Share on other sites

Haha, what a damn cool trick. That's superb! biggrin_o.gif

Share this post


Link to post
Share on other sites

Hi,

A very very useful collection.

Thank you for this extraordinary work smile_o.gif

bye

Silola

Share this post


Link to post
Share on other sites

It surely is a great bunch of functions! Kuddos, and gratitude smile_o.gif

Share this post


Link to post
Share on other sites

Now that we have some more string functions available in ArmA I have updated this library to take advantage of it - and to make it about 100 times faster... smile_o.gif

The calling syntax is still the same (even though some functions aren't really necessary anymore, I left them in for compatibility reasons).

A few new things:

(All string positions is 0-based, i.e. the first position in the string is 0.)

<span style='font-size:10pt;line-height:100%'>StrMid</span>

Can now also accept just a single parameter (the position). In that case the string from that position on is returned.<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">_mid=["abcdefgh",3] call KRON_StrMid;

_mid will contain "defgh"

<span style='font-size:10pt;line-height:100%'>StrIndex</span>

Returns the position of a sub-string.<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">_index=["abcdefg","cd"] call KRON_StrIndex;

_index will be 2

<span style='font-size:10pt;line-height:100%'>Replace</span>

Replaces every occurrence of a sub-string. <table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">_new=["abcabc","b","XXX"] call KRON_Replace;

_new will contain: "aXXXcaXXXc"

<span style='font-size:10pt;line-height:100%'>FindFlag</span>

Searches a mixed array for a string, and returns true or false, depending on presence.

The main purpose of this is to search the parameter array (_this) for a specific flag. Case-insensitive.<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">nul=[1,100,"Slow",false] exec "test.sqf";

Inside test.sqf:

_flg=[_this,"slow"] call KRON_FindFlag;

_flg will be true

<span style='font-size:10pt;line-height:100%'>getArg</span>

Returns an "argument" that has been supplied with a specific tag.

The concept behind this is a bit more complicated to explain, but again, the main purpose is to parse the parameter array (_this).

Instead of having to enter arguments at specific positions in the parameter array, they can now be supplied anywhere in the array.

i.e. all the following calls would return the same values if they are parsed as follows:<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">_min=parseNumber([_this,"min"] call KRON_getArg);

_med=parseNumber([_this,"med"] call KRON_getArg);

_max=parseNumber([_this,"max"] call KRON_getArg);

will get the same results with any of these calls:

nul=[this,"min:100","max:200","med:150"] exec "script.sqf"

nul=[this,"MIN:100","Med:150","max:200"] exec "script.sqf"

nul=[this,"Max:200","Min:100","Med:150"] exec "script.sqf"

<span style='font-size:10pt;line-height:100%'>Compare</span>

Compares two strings. Returns -1 if first arguments is smaller, 1 if second is smaller, and 0 if they are equal. Optional case-sensitivity via "case" flag.

(Order is: numbers, uppercase, lowercase).<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">_cmp=["bbb","aaa"] call KRON_Compare;

_cmp will be 1 (2nd element is smaller)

<span style='font-size:10pt;line-height:100%'>ArraySort</span>

Sorts a multi-dimensional array.

Pretty straightforward for single-dimensional arrays:<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">["bbb","aaa","ccc"] will turn into ["aaa","bbb","ccc"]

A bit more complicated for multi-dimensional ones, as you can specify which column to sort on:<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">_arr=[["bbb",2],["aaa",3],["ccc",1]];

_srt=[_arr] call KRON_ArraySort;

_srt will contain [["aaa",3],["bbb",2],["ccc",1]]

If I specify '1' as the sort column (the numbers) it will look like this:<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">_arr=[["bbb",2],["aaa",3],["ccc",1]];

_srt=[_arr,1] call KRON_ArraySort;

_srt will contain [["ccc",1],["bbb",2],["aaa",3]]

With the optional parameter "desc" the sort order is reversed:<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">_arr=[["bbb",2],["aaa",3],["ccc",1]];

_srt=[_arr,1,"desc"] call KRON_ArraySort;

_srt will contain [["aaa",3],["bbb",2],["ccc",1]]

And with the optional parameter "case" capitalization is taken into account (order is: numbers, uppercase, lowercase).

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">_arr=[["bbb",2],["aaa",3],["CCC",1]];

_srt=[_arr,"case"] call KRON_ArraySort;

_srt will contain [["CCC",1],["aaa",3],["bbb",2]]

Download location is in the first post.

Share this post


Link to post
Share on other sites

Excellent work. The sorting and searching functions will be quite useful.

Presumably this now means there are no more character limitations like the original version. Eg: I wasn't able to search for quote characters in strings.

I'm still needing a parameter substitution (find/replace) command to replace parameters in a string with another word - for use by the popup menu dialogs.

It's currently using %1...%22 for various substitutions of positions, grid refs, ID's, etc, but this prevents you from nesting format commands, plus it's a little unreadable.

It needs to handle the same substitution string multiple times in the same string (eg: 'abc %1 def %1 ghi' ).

That would be handy in future. Otherwise I'll post one up when I get around to it eventually.

Edit: Awesome, I see the new Replace function in ver 2.1. Thanks.

Share this post


Link to post
Share on other sites

I *knew* I forgot something... wink_o.gif

Check out the added "Replace" function.

It will replacement every occurrence of a string; and the replacement string can have a different size than the replaced one (it can even be empty, in which case the original strings will be removed).

Hopefully that'll do the job.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×