There are several string operations available to modify, select, and combine strings. Furthermore, some operations allow you to convert numbers (real and integer) to strings.
| Operation | Meaning | HALCON operator |
| v$s | convert v using specification s | tuple_string |
| v1 + v2 | concatenate v1 and v2 | tuple_add |
| strchr(s1,s2) | search character s2 in s1 | tuple_strchr |
| strstr(s1,s2) | search substring s2 in s1 | tuple_strstr |
| strrchr(s1,s2) | search character s2 in s1 (reverse) | tuple_strrchr |
| strrstr(s1,s2) | search substring s2 in s1 (reverse) | tuple_strrstr |
| strlen(s) | length of string | tuple_strlen |
| str_firstn(s,i) | cut the first characters of s up to position i | tuple_str_first_n |
| str_lastn(s,i) | cut the characters of s from position i | tuple_str_last_n |
| s{i} | select character at position i; 0 <= i <= strlen(s)-1 | tuple_str_bit_select |
| s{i1:i2} | select substring from position i1 to position i2 | tuple_substr |
| split(s1,s2) | split s1 in substrings at s2 | tuple_split |
| regexp_match(s1,s2) | extract substrings of s1 matching the regular expression s2 | tuple_regexp_match |
| regexp_replace(s1,s2,s3) | replace substrings of s1 matching the regular expression s2 with s3 | tuple_regexp_replace |
| regexp_select(s1,s2) | select tuple elements from s1 matching the regular expression s2 | tuple_regexp_select |
| regexp_test(s1,s2) | return how many tuple elements in s1 match the regular expression s2 | tuple_regexp_test |
| s1 =~ s2 | return how many tuple elements in s1 match the regular expression s2 | tuple_regexp_test |
Table 8.13: String operations.
See also: tuple_string.
$ converts numbers to strings or modifies strings. The operation has two operands: The first one (left of the $) is the number that has to be converted. The second one (right of the $) specifies the conversion. It is comparable to the format string of the printf() function in the C programming language. This format string consists of the following four parts
<flags><field width>.<precision><conversion>
or as a regular expression:
[-+ #]?([0-9]+)?(\.[0-9]*)?[doxXfeEgGsb]?
(which roughly translates to zero or more of the characters in the first bracket pair followed by zero or more digits, optionally followed by a dot which may be followed by digits followed by a conversion character from the last bracket pair).
Some conversion examples might show it best:
| Input | Output |
| 23 $ '10.2f' | ' 23.00' |
| 23 $ '-10.2f' | '23.00 ' |
| 4 $ '.7f' | '4.0000000' |
| 1234.56789 $ '+10.3f' | ' +1234.568' |
| 255 $ 'x' | 'ff' |
| 255 $ 'X' | 'FF' |
| 0xff $ '.5d' | '00255' |
| 'total' $ '10s' | ' total' |
| 'total' $ '-10s' | 'total ' |
| 'total' $ '10.3' | ' tot' |
In no case does a nonexistent or insufficient field width cause truncation of a field; if the result of a conversion is wider than the field width, the field is simply expanded to contain the conversion result.
Examples for the string conversion can be found in the program string.hdev.
See also: tuple_add.
The string concatenation (+) can be applied in combination with strings or all numerical types; if necessary, the operands are first transformed into strings (according to their standard representation). At least one of the operands has to be already a string so that the operator can act as a string concatenator. In the following example a file name (e.g., 'Name5.tiff') is generated. For this purpose two string constants ('Name' and '.tiff') and an integer value (the loop-index i) are concatenated:
for i := 1 to 5 by 1
read_image (Image, 'Name'+i+'.tiff')
endfor
See also: tuple_strchr, tuple_strrchr.
str(r)chr(s1,s2) returns the index of the first (last) occurrence of one of the character in s2 in string s1, or -1 if none of the characters occur in the string. s1 may be a single string or a tuple of strings.
See also: tuple_strstr, tuple_strrstr.
str(r)str(s1,s2) returns the index of the first (last) occurrence of string s2 in string s1, or -1 if s2 does not occur in the string. s1 may be a single string or a tuple of strings.
See also: tuple_strlen.
strlen(s) returns the number of characters in s.
See also: tuple_str_first_n.
str_firstn(s,t) returns the characters from the beginning of string s up to position t.
See also: tuple_str_last_n.
str_lastn(s,t) returns the character from position t of string s to the end.
See also: tuple_str_bit_select, tuple_substr.
s{i} selects a single character (specified by index position) from s. The index ranges from zero to the length of the string minus 1. The result of the operator is a string of length one.
s{i1:i2} returns all characters from the first specified index position (i1) up to the second specified position (i2) in s as a string. The index ranges from zero to the length of the string minus 1.
See also: tuple_split.
split(s1,s2) divides the string s1 into single substrings. The string is split at those positions where it contains a character from s2. As an example the result of
split('/usr/image:/usr/proj/image',':')
consists of the two strings
['/usr/image','/usr/proj/image']
HDevelop provides string functions that use Perl compatible regular expressions. Detailed information about them can be found in the Reference Manual at the descriptions of the corresponding operators, which have the same name but start with tuple_. In particular, at the description of tuple_regexp_match you find further information about the used syntax, a list of possible options, and a link to suitable literature about regular expressions.
See also: tuple_regexp_match.
regexp_match(s1,s2) searches for elements of the tuple s1 that match the regular expression s2. It returns a tuple with the same size as the input tuple (exceptions exist when working with capturing groups, see the description of tuple_regexp_match in the Reference Manual for details). The resulting tuple contains the matching results for each tuple element of the input tuple. For a successful match the matching substring is returned. Otherwise, an empty string is returned.
See also: tuple_regexp_replace.
regexp_replace(s1,s2,s3) replaces substrings in s1 that match the regular expression s2 with the string given in s3. By default, only the first matching substring of each element in s1 is replaced. To replace all occurrences, the option 'replace_all' has to be set in s2 (see tuple_regexp_replace).
For example:
assign(regexp_replace(List, '\\.jpg$', '.png'), List)
substitutes file names that look like JPEG images with PNG images.
See also: tuple_regexp_select.
regexp_select(s1,s2) returns only the elements of the tuple s1 that match the regular expression s2. In contrast to regexp_match, the original tuple elements instead of the matching substrings are returned. Tuple elements that do not match the regular expression are discarded.
For example:
assign(regexp_select(List, '\\.jpg$'), Selection)
sets Selection to all the strings from List that look like file names of JPEG images. Please note that the backslash character has to be escaped to be preserved.
See also: tuple_regexp_test.
regexp_test(s1,s2) returns the number of elements of the tuple s1 that match the regular expression s2. Additionally, a short-hand notation of the operator is available, which is convenient in conditional expressions:
s1 =~ s2