PilotScript String Functions

The following string functions are available for PilotScript components.

String Function Name/Prototype Description Example

Asc

Number Asc(String char)
Returns the Unicode or ASCII decimal code that represents the first character of the string. The returned code is a decimal integer, whereas the Unicode code charts show the equivalent hex values.
Asc('A');

Returns 65

Asc("\n");

Returns 10

Chr

String Chr(Number charCode)
Returns the Unicode or ASCII character represented by charCode. charCode is a decimal integer, whereas the code charts show the equivalent hex values.
Chr(65);

Returns "A"

FindLastSubstring

Number FindLastSubstring(String str, String sub[, Boolean ignorecase=false])
Returns the index of the last occurrence of the string sub in the string str. Returns zero (0) if the substring is not found. By default, this is a case-sensitive search. However, if you provide the argument ignorecase with the value True, case is ignored.
#str := 'The first fox chased the second fox.'; 

#lastfox := FindLastSubstring(#str, 'fox'); 
#lastdog := FindLastSubstring(#str, 'dog');

After the above script is executed, #lastfox contains the value 33 and #lastdog contains the value 0.

FindSubstring

Number FindSubstring(String str, String sub[, Number start=1, Boolean ignorecase=false])
Returns the index of the first occurrence of the string sub in the string str (optionally starting at character start). Returns zero (0) if the substring is not found. By default, this is a case-sensitive search. However, if you provide the argument ignorecase with the value True, case is ignored.
#str := 'The first fox chased the second fox.';

#fox := FindSubstring(#str, 'fox'); 

#dog := FindSubstring(#str, 'dog');

After the above script is executed, #fox contains the value 11 and #dog contains the value 0.

Format

String Format(String fmt, ...)

Returns a string formatted as described by the fmt parameter.

The Format() function is modeled after the C sprintf() function. Format returns a formatted string. Each field of the format specification is a single character or a number signifying a particular format option. The simplest format specification contains only the percent sign (%) and a type character (for example, %s).

The format_string is composed of zero or more directives:

%[flags] [width] [.precision] type 
string := Format (format_string [, mixed args...]); 

Returns a string produced according to the formatting format_string.

See Format function options for a brief description of the optional fields that control various aspects of the formatting.

Type is a required character that determines whether the associated argument is interpreted as a character, string, or number.

If no argument is required, use the literal percent character (%). Otherwise, arguments are presented as described below:

Format function options:

c: Character with that ASCII value.

d: Signed integer.

e, E: Treated as a double; presented in scientific format.

f: Floating-point number.

g, G: General format.

i: Signed integer.

o: Octal number.

s: String.

x, X: Hexadecimal number, with lowercase/uppercase letters.

Flags: One or more optional characters that control justification of output and printing of signs, blanks, decimal points, and octal and hexadecimal prefixes. More than one flag can appear in a format specification. Flags can include the following:

-: Left aligns the result within a given field width.

+: Left aligns the result within a given field width.

o: If width is prefixed with 0, zeros are added until a minimum width is reached. (If 0 and - appear, the 0 is ignored). The 0 is ignored if specified with one of the following formats: u, x, X, o.

blank (' '): Prefixes the output value with a blank, if the output value is signed and positive. The blank is ignored if both the blank and + flags appear .

#: When used with the o, x, or X format, the # flag prefixes any nonzero output value with 0, 0x, or 0X, respectively ( # ).

Width: Optional number that specifies the minimum number of characters generated.

Precision: Optional number that specifies the maximum number of characters printed for all or part of the output field, or the minimum number of digits printed for integer values.

Floating Point Precision:

activity := 4.017345;

activity := format('%5.2f', activity);

/* activity is now stored as the string ' 4.02'. */

The leading space created as padding makes the total width five characters.

Concatenating Integers as Strings:

plate := 12;

row := 5;

col := 9;

id := format('ID%04d%02d%02d', plate, row, col);

/* id is now stored as 'ID00120509' */

Zero-Padded Integers:

Year := 2001;

Month := 3;

Day := 6;

Date := format('%04d-%02d-%02d', year, month, day);

/* Date is now stored as '2001-03-06' */

Hashcode

Number Hashcode(Value str)
Returns a hash code for the value str as an integer value.
Hashcode('benzene');

Returns -222085843

RMatch

StringArray RMatch(String str, String regexp[, String options])

Similar to the m// operator in Perl. Returns an array of the substrings of str that match the regular expression pattern regexp. When evaluated in the context of a number, returns the number of times the regular expression matches the string, provided that the g option is used. When evaluated in a Boolean context, returns True, if regexp matches anywhere in the string str.

RMatch function options:

The RMatch options parameter is a string that modifies the behavior of the regular expression match. It can contain any combination of the following characters (these options are identical to the regular expression options in Perl):

g: Do "global" matching (to match all occurrences of the pattern). If not specified, only the first occurrence in the string is matched.

i: Do case-insensitive pattern matching.

m: Treat the string as multiple lines.

s: Treat the string a single line.

x: Extend a pattern's legibility by permitting whitespace and comments.

With grouping, RMatch() returns an array. The first element is the full pattern matched; subsequent elements are the patterns matched by each group specified in the query.

prop1 := 'aaaaaxbbbbbbbbycccccccccz'; 
prop2 := rmatch(prop1, '(^.*)(x)(.*)(y)(.*)(z$)');

Returns:

prop2[] 
aaaaaxbbbbbbbbycccccccccz 
aaaaa 
x 
bbbbbbbb 
y 
 ccccccccc 
z

If the g option is used, only the full pattern match is returned, none of the group matches.

prop1 := '12a34b56a78b'; 

prop2 := RMatch(prop1, '(\d)(\d)a','g');

RSplit

StringArray RSplit(String str, String regexp[, String options[, Number max]])
Similar to the split function in Perl. Returns an array of substrings of str split at places where str matches the regular expression pattern regexp. The options string is described above with the Rmatch function. If max is specified, then the first max substrings are returned.
/* Example using the max option */

Days := "Sun Mon Tue Wed Thu Fri Sat Sun";

firstDays := rsplit(days, '\s', 'g', 2);

firstDays is an array of two elements. The first element is Sun and the second element is Mon.

RSubst

String RSubst(String str, String regexp, String repl[, String options])

Similar to the s// operator in Perl. Substitutes patterns in str matched by the regular expression regexp with the replacement pattern string repl and returns the new string value.

RSubst function options:

The RSubst function uses the same options described above for RMatch. The options can contain any combination of the following characters (these options are identical to the regular expression options in Perl):

g: "global" matching (to match all occurrences of the pattern). If not specified, only the first occurrence in the string is matched.

i: Do case-insensitive pattern matching.

m: Treat the string as multiple lines.

s: Treat the string a single line.

x: Extend a pattern's legibility by permitting whitespace and comments.

Like the Perl regular expression engine, RSubst supports parentheses and indexed $ tokens to use sections of the replaced text in replacement text. See the examples for clarification. In addition to $1, $2, etc. tokens, you can use $` and $' in the replacement string, which represent the text of the input string that lies before the matching section and the text that lies after it (as in Perl).

NewFileName := RSubst(Filename, '\.(txt|csv)', '.xml', 'i');

Replace file extensions '.txt' and '.csv' with '.xml'.

AbbreviatedDay := RSubst(DateText, '(\w{3})\w*day', '$1');

Abbreviates to 3 letters the day of the week in a date string (in English, at least!)

ext := RSubst(Text, '(^|\s)scitegic($|\W)', '$1BIOVIA$2', 'ig');

Replace all instances of the isolated word 'scitegic' (any case) with 'BIOVIA'.

StrErase

None StrErase(String str, Number start[, Number len])
Erases a substring within str, starting at position/index start with (optional) length len. (If the length is not given, the string is erased to the end.) The string is edited in place. This function cannot operate on arbitrary elements of arrays. If the first argument is the name of an array-valued property or global property, only the first element is affected.
str := 'The quick brown fox jumps over the lazy dog.'; 
#jumps := FindSubstring(str, 'jumps'); 
StrErase(str, #jumps + 5); 

After the above script is executed, the property str contains the value "The quick brown fox jumps".

StringLen

Number StringLen(String str)
Returns the number of characters in the string str. (Equivalent to StrLength; both are included so you don't have to remember which is which)
StringLen("Dogs bark.");

Returns 10

StrInsert

None StrInsert(String str, Number start, String insertStr)
Inserts a string insertStr into the string str, starting at character start. The string is edited in place. This function cannot operate on arbitrary elements of arrays. If the first argument is the name of an array-valued property or global property, only the first element is affected.
str := 'The quick brown fox jumps over the lazy dog.'; 
#fox := FindSubstring(str, 'fox'); 
StrInsert(str, #fox - 1, ', dirty');

After the above script is executed, the property str contains the value "The quick brown, dirty fox jumps over the lazy dog."

StrLength

Number StrLength(String str)
Returns the number of characters in the string str. (Equivalent to StringLen; both are included so you don't have to remember which is which!)
StrLength("Dogs bark.");

Returns 10

StrOverwrite

None StrOverwrite(String str, Number start, String overwriteStr)
Overwrites using the string overwriteStr onto the string str, starting at character start. If the overwrite string is longer than the number of characters remaining in str, the string is extended in length. This function cannot operate on arbitrary elements of arrays. If the first argument is the name of an array-valued property or global property, only the first element is affected.
str := 'MMM 1 is the first day.'; 
StrOverwrite(str, 1, 'Apr');

After the above script is executed, the property str contains the value "Apr 1 is the first day."

StrReplace

String StrReplace(String str, String substr, String replace[, Number num])
Replaces all occurrences of the substring substr in the string str with the string replace, and returns the result as a new string. The string str is unchanged. If the optional num parameter is specified, only the first num occurrences of substr are replaced.
str := 'The first fox chased the second fox.'; 

str := StrReplace(str, 'fox', 'squirrel');

After the above script is executed, the property str contains the value "The first squirrel chased the second squirrel."

Substring

String Substring(String str, Number start[, Number len])
Returns the substring of str, starting at character start with (optional) length len.
Substring('one two three', 5, 3);

Returns the value "two".

ToLower

String ToLower(String str)
Returns the string value str in all lowercase letters.
ToLower('BIG little');

Returns the value "big little".

ToUpper

String ToUpper(String str)
Returns the string value str in all uppercase letters.
ToUpper('BIG little');

Returns the value "BIG LITTLE"

Trim

String Trim(String str[, String options])

Returns a string with the leading and trailing whitespace of the given string str removed.

Trim function options:

i: Trim internal whitespace (reduces multiple-character whitespace to a single space).

l: Trim left-most whitespace (removes all whitespace to the left of the first non-whitespace character).

r: Trim right-most whitespace (removes all whitespace to the right of the last non-whitespace character).

If no option string is specified, lr (left- and right-most whitespace) is assumed.

Trim('  one two three  ');

Returns the value "one two three"

URLDecode

String URLDecode (String str)
Returns the URL decoded as a string according to RFC 1738.
/* Decode the URL to remove encoded characters. */ 
string := URLDecode("http://www.google.com/search?q=BIOVIA"); 

URLEncode

String URLEncode(String str)
Returns the string encoded as a URL according to RFC 1738.
/* Encode the URL to handle white spaces and other special characters. */ 

searchURL := "http://www.google.com/search?q=" . URLEncode(stringToSearch);