The StringUtils task provides a number of useful string manipulation functions, such as converting a string to upper or lower case, trimming white space, finding a substring, etc. Caution: this task does not follow the standard Ant convention of property immutability.
To use this task in your build files, include a task definition like this:
<taskdef name="stringutil" classname="ise.antelope.tasks.StringUtilTask"/>
Table 13.1. StringUtil Task Attributes
Attribute | Description | Default | Required |
---|---|---|---|
string | The string to manipulate. | None | No |
property | Where to store the manipulated string. Caution: this task will overwrite any existing property with this name. | None | No |
Operations as nested elements:
Table 13.2. indexof, lastindexof: Find the index of or the last index of a substring.
Attribute | Description | Default | Required |
---|---|---|---|
string | The string to find the index of. | None | No |
fromindex | Where to start looking. | 0 | No |
Table 13.3. substring: Get a substring from the string.
Attribute | Description | Default | Required |
---|---|---|---|
beginindex | Start of substring. | 0 | No |
endindex | End of substring. | End of string. | No |
Table 13.4. replace: Replace parts of the string.
Attribute | Description | Default | Required |
---|---|---|---|
regex | Pattern to replace. | None | No |
replacement | What to replace with. | None | No |
Table 13.5. sort: Sort items in a string.
Attribute | Description | Default | Required |
---|---|---|---|
separator | The character separating individual items in the string. | Any of tab, new line, carriage return, line feed, or space. | No |
This is useful for sorting a comma separated list or a property value that spans several lines.
Table 13.6. messagebox: Put the string in a box for nice display.
Attribute | Description | Default | Required |
---|---|---|---|
title | Title for the message box. | None | No |
width | Maximum width in characters for the message box. Lines will be wrapped to fit inside the box. The box will not stretch to this width if the message is less than width - 4 characters wide. | 60 | No |
These operations have no attributes:
lowercase Converts the string to lowercase.
uppercase Converts the string to uppercase.
length Finds the length of the string.
trim Removes white space from boths ends of the string.
More than one of the operations can be used at once, that is, you can convert a string to lowercase and trim it at the same time. See below for examples.
Examples:
Convert a string to lower case:
<target name="test1"> <property name="prop1" value="ABCDE"/> <echo>prop1 before = ${prop1}</echo> <a:stringutil string="${prop1}" property="prop1"> <a:lowercase/> </a:stringutil> <echo>prop1 after = ${prop1}</echo> <a:assert message="prop1, expected abcde, got ${prop1}"> <bool> <equals arg1="abcde" arg2="${prop1}"/> </bool> </a:assert> </target>
Convert a string to upper case:
<target name="test2"> <property name="prop2" value="abcdefgh"/> <echo>prop2 before = ${prop2}</echo> <a:stringutil string="${prop2}" property="prop2"> <a:uppercase/> </a:stringutil> <echo>prop2 after = ${prop2}</echo> <a:assert message="prop2, expected ABCDEFGH, got ${prop2}"> <bool> <equals arg1="ABCDEFGH" arg2="${prop2}"/> </bool> </a:assert> </target>
Convert a string to upper case and trim white space:
<target name="test3"> <property name="prop3" value=" abcdefgh "/> <echo>prop3 before = ${prop3}</echo> <a:stringutil string="${prop3}" property="prop3"> <a:uppercase/> <a:trim/> </a:stringutil> <echo>prop3 after = ${prop3}</echo> <a:assert message="prop3, expected ABCDEFGH, got ${prop3}"> <bool> <equals arg1="ABCDEFGH" arg2="${prop3}"/> </bool> </a:assert> </target>
Convert a string to upper case, trim white space, and find a substring:
<target name="test4"> <property name="prop4" value=" abcdefgh "/> <echo>prop4 before = ${prop4}</echo> <a:stringutil string="${prop4}" property="prop4"> <a:uppercase/> <a:trim/> <a:substring beginindex="3"/> </a:stringutil> <echo>prop4 after = ${prop4}</echo> <a:assert message="prop4, expected DEFGH, got ${prop4}"> <bool> <equals arg1="DEFGH" arg2="${prop4}"/> </bool> </a:assert> </target>
Get the length of a string:
<target name="test7"> <property name="prop7" value="abcdefgh"/> <echo>prop7 before = ${prop7}</echo> <a:stringutil string="${prop7}" property="prop7"> <a:length/> </a:stringutil> <echo>prop7 length = ${prop7}</echo> <a:assert message="prop7, expected 8, got ${prop7}"> <bool> <mathequals arg1="8" arg2="${prop7}"/> </bool> </a:assert> </target>
Sort a list:
<target name="test12"> <property name="prop12" value="a,b,c,d,e,f,g,h,a,b,c,d,e,f,g,h"/> <echo>prop12 before = ${prop12}</echo> <a:stringutil string="${prop12}" property="prop12"> <a:sort separator=","/> </a:stringutil> <echo>prop12 sorted = ${prop12}</echo> <a:assert message="prop12, expected 'a,a,b,b,c,c,d,d,e,e,f,f,g,g,h,h', got ${prop12}"> <bool> <equals arg1="a,a,b,b,c,c,d,d,e,e,f,f,g,g,h,h" arg2="${prop12}"/> </bool> </a:assert> </target>
Message boxes:
<target name="test13"> <property name="prop13" value="Character boundary analysis allows users to interact with characters as they expect to, for example, when moving the cursor around through a text string. Character boundary analysis provides correct navigation of through character strings, regardless of how the character is stored. For example, an accented character might be stored as a base character and a diacritical mark. What users consider to be a character can differ between languages."/> <echo>prop13 before = ${prop13}</echo> <a:stringutil string="${prop13}" property="prop13"> <a:messagebox/> </a:stringutil> <echo>prop13 in messagebox:${line.separator}${prop13}</echo> <property name="prop13a" value="Character boundary analysis allows users to interact with characters as they expect to, for example, when moving the cursor around through a text string. Character boundary analysis provides correct navigation of through character strings, regardless of how the character is stored. For example, an accented character might be stored as a base character and a diacritical mark. What users consider to be a character can differ between languages."/> <a:stringutil string="${prop13a}" property="prop13a"> <a:messagebox title="About Character Boundaries"/> </a:stringutil> <echo>prop13a in messagebox with title:${line.separator}${prop13a}</echo> </target> test13: [echo] prop13 before = Character boundary analysis allows users to interact with characters as they expect to, for example, when moving the cursor around through a text string. Character boundary analysis provides correct navigation of through character strings, regardless of how the character is stored. For example, an accented character might be store d as a base character and a diacritical mark. What users consider to be a character can differ between languages. [echo] prop13 in messagebox: [echo] [echo] +----------------------------------------------------------------------+ [echo] | Character boundary analysis allows users to interact with characters | [echo] | as they expect to, for example, when moving the cursor around | [echo] | through a text string. Character boundary analysis provides correct | [echo] | navigation of through character strings, regardless of how the | [echo] | character is stored. For example, an accented character might | [echo] | be stored as a base character and a diacritical mark. What users | [echo] | consider to be a character can differ between languages. | [echo] +----------------------------------------------------------------------+ [echo] prop13a in messagebox with title: [echo] [echo] +----------------------------------------------------------------------+ [echo] | About Character Boundaries | [echo] +----------------------------------------------------------------------+ [echo] | Character boundary analysis allows users to interact with characters | [echo] | as they expect to, for example, when moving the cursor around | [echo] | through a text string. Character boundary analysis provides correct | [echo] | navigation of through character strings, regardless of how the | [echo] | character is stored. For example, an accented character might | [echo] | be stored as a base character and a diacritical mark. What users | [echo] | consider to be a character can differ between languages. | [echo] +----------------------------------------------------------------------+ [antlib:ise.antelope.tasks:testcase] test13 passed.