String expressions can be parsed. Different from numeric expressions, string expressions will return values of the string type. The return values cannot be calculated as numbers but can only be used as parameters in string functions. String expressions are mainly used to perform different string operations. In a string expression, only functions supporting string expressions, strings, string variables, and numeric variables can be nested. In other functions, parameters that are of the string type and support expressions can call the string expression parsing function.
Arrays: An example is @string[expression]. The index can be parsed using an expression.
Global variables of the number and string types can be parsed by using #num and @str respectively. To perform numeric calculation in a string expression, do not start with a variable such as #num. For example, #num*10 will be considered as a numeric variable named num*10. Instead, write 10*#10.
String functions listed in the following table are supported.
The string in a string expression must be enclosed in single quotation marks, for example, @a + 'hello' + @b. Plus signs (+) are used to concatenate strings in a string expression. That is, when parsing a string expression, the system splits it based on plus signs (+), parses the sub string expressions, and concatenates return values of the sub string expressions to obtain the final return value. The plus sign (+) does not support value addition.
Expression | Description |
|---|---|
substr(Original string,Start position of the string,String length) | Returns a substring. For example, the return value of substr('Hello',1,2) is el. (The position of a character is counted from left to right and the position index starts from 0.) |
strIsEmpty(str) | Checks whether a string is empty. If yes, true is returned. If no, false is returned. An expression example is strIsEmpty(@abc). In the expression, str can be a string, string variable, or string function. This rule also applies to the parameter in the following expressions. |
strIndexOf(str1,str2) | Returns the position index of the first str2 in str1. The index value starts from 0. For example, the return value of strIndexOf('hello','he') is 0. If str2 does not exist in str1, –1 is returned. |
strLastIndexOf(str1, str2) | Returns the position index of the last str2 in str1. The index value starts from 0. For example, the return value of str_LastIndexOf('hello','l') is 3. If str2 does not exist in str1, –1 is returned. |
strContains(str1,str2) | Checks whether str1 contains str2. If yes, true is returned. If no, false is returned. For example, the return value of strContains('hello','he') is true. |
strReplaceAll(str1,str2,str3) | Replaces all str2 in str1 with str3. For example, the return value of (strReplaceAll('hello','e','i') is hillo. |
preciseeval(str,Number/Numeric variable) | Calculates the value of str. In this expression, str can be a string, string variable, or numeric function, and Number/Numeric variable indicates a number or numeric variable that indicates the number of decimal places to be reserved. For example, the return value of preciseeval('1/3',3) is 0.333. The preciseeval function cannot be followed by other functions or the connection sign (+). |
formatDate('format',@time_sys) | Returns the current system time string in a specified format. For example, the return result of formatDate('yyyy-MM-dd hh:mm:ss',@time_sys) is 2019-08-23 19:15:10. For details about the time and date formats that are supported, please refer to <DateTime>. |
plus(a,b) | Returns the integer sum of a and b. In the expression, a and b can be a string, string variable, number, numeric variable, or string function whose return value is a number. For example, the return value of plus(3,3) is 6. |
ifelse(x1,y1,x2,y2,...,xn,yn,z) | if (x1!=0) return y1; elseif(x2 !=0) return y2;...; else return z Checks whether xi is unequal to 0, where i starts from 1. If yes, yi is returned. If no, the judgment continues until a condition is met. If all the x values are equal to 0, z is returned finally. In the expression, xi is a numeric expression, and yi and z are string results, which can be strings, string variables, or string functions. |
+ | Concatenates strings, for example, #year+'/'+#month+'/'+#date. The strings must be enclosed in single quotation marks. |
Example 1: Concatenate strings and numeric or string variable values. For example, if the date is 20191001, the return value is 2019/10/01.
<Text x="650" y="1650" size="30" textExp="#year+'/'+#month+'/'+#date"/>
Example 2: Extract and return a substring.
<Var name="a" expression="hello hello hello hello" type="string"/> <Var name="k" expression="3"/> <!--Starting from index 0, extract three characters from the string. Then the substring hel is obtained.--> <Text x="50" y="0" size="30" format="substr(@a,0,3):%s" paras="substr(@a,0,3)"/> <!--Starting from index 0, extract 12 characters from @a based on the inner substr expression. Then, use the substring obtained as the string to be truncated based on the outer substr expression and extract 5 characters starting from #k (that is, 3). Finally, lo he is returned.--> <Text x="50" y="0" size="30" format="substr(substr(@a,0,12),#k,5):%s" paras="substr(substr(@a,0,12),#k,5)"/>
Example 3: Calculate the value of a string and reserve a specified number of decimal places.
<Var name="k" expression="3"/>
<!--Divide 1 by 3 and retain three decimal places. Then 0.333 is returned.-->
<Text x="350" y="700" size="30" format="preciseeval('1/3',3):%s" paras="preciseeval('1/3',3)"/>
<!--The first parameter supports numeric functions. In this example, –1 divided by 3 is less than 1. Therefore, the return value of the lt function is 1. Based on the second parameter #k whose value is 3, three decimal places need to be reserved. Finally, 1.000 is returned.-->
<Text x="50" y="600" size="30" format="preciseeval(lt(-1/3,1),#k):%s" paras="preciseeval(lt(-1/3,1),#k)"/> Example 4: Format the current time to display the hour and minute. The return value varies according to whether the 12- or 24-hour clock is used. If the current time is 9:00 PM, the return value is 09:00 when the 12-hour clock is used and 21:00 when the 24-hour clock is used.
<Text x="350" y="450" size="30" format="formatDate('HH:mm',@time_sys):%s" paras="formatDate('HH:mm',@time_sys)"/> Example 5: Check whether string a contains string 10. In this example, false is returned, indicating that string a does not contain string 10.
<Var name="a" expression="hello hello hello hello" type="string"/> <Text x="50" y="1450" size="30" format="strContains(@a,'10'):%s" paras="strContains(@a,'10')"/>
Example 6: Use the argb function to parse the values of transparency, red, green, and blue, and return an 8-digit hexadecimal color code.
<Var name="c" expression="asdfghjklqwertyuiopxcvbnm,sdfghjklwertyuiopdfghjkxcvbnmfghjrtyui" type="string"/> <Var name="h" expression="100"/> <Var name="i" expression="100"/> <Var name="j" expression="100"/> <!--The values of the h, j, and k variables are all 100. Therefore, the return value of the strLastIndexOf function is 63. Finally, #643f6464 is returned.--> <Text x="50" y="50" size="30" color="argb(#h,strLastIndexOf(@c,'i'),#i,#j)" text="argb(#h,strLastIndexOf(@c,'i'),#i,#j)"/>
Example 7: Use the strIsEmpty function to check whether a string is empty.
<Var name="a" expression="hello hello hello hello" type="string"/> <Var name="d" expression="" type="string"/> <!--The string @a is not empty. Therefore, false is returned.--> <Text x="600" y="100" size="30" format="strIsEmpty(@a):%s" paras="strIsEmpty(@a)"/> <!--The string @d is empty. Therefore, true is returned.--> <Text x="600" y="150" size="30" format="strIsEmpty(@d):%s" paras="strIsEmpty(@d)"/> <!--The return result of substr(@a,0,3) is hel. Therefore, false is returned.--> <Text x="50" y="100" size="30" format="strIsEmpty(substr(@a,0,3)):%s" paras="strIsEmpty(substr(@a,0,3))"/> <!--The return result of substr(@d,0,3) is empty. Therefore, true is returned.--> <Text x="50" y="150" size="30" format="strIsEmpty(substr(@d,0,3)):%s" paras="strIsEmpty(substr(@d,0,3))"/>
Example 8: Use the ifelse function for condition judgment.
<Var name="c" expression="asdfghjklqwertyuiopxcvbnm" type="string"/> <!--The string @c contains as. Therefore, the strContains function returns 1, and finally the ifelse function returns 2.--> <Text x="50" y="650" size="30" format="ifelse(strContains(@c,'as'),2,3):%s" paras="ifelse(strContains(@c,'as'),2,3)"/> <!--The string @c does not contain hello. Therefore, the strContains function returns 0, and finally the ifelse function returns bb.--> <Text x="50" y="700" size="30" format="ifelse(strContains(@c,'hello'),'aa','bb'):%s" paras="ifelse(strContains(@c,'hello'),'aa','bb')"/>
Example 9: Use the plus function to implement the addition function.
<Var name="i" expression="100"/>
<Var name="j" expression="100"/>
<Var name="a" expression="hello hello hello hello" type="string"/>
<!--The values of i and j are both 100. Their sum is 200. Therefore, 200 is returned.-->
<Text x="50" y="900" size="30" format="plus(#i,#j):%s" paras="plus(#i,#j)"/>
<!--The return value of the strIndexOf function is 2. Therefore, 3 is returned finally.-->
<Text x="50" y="950" size="30" format="plus(strIndexOf(@a,'l'),1):%s" paras="plus(strIndexOf(@a,'l'),1)"/>
<!--The return values of the strIndexOf function are 0 and 1 respectively. Therefore, 1 is returned finally.-->
<Text x="50" y="1000" size="30" format="plus(strIndexOf('asdfg','as'),strIndexOf('asdfg','sd')):%s" paras="plus(strIndexOf('asdfg','as'),strIndexOf('asdfg','sd'))"/>