Madden
Madden

Reputation: 1080

ANT: Remove leading/trailing whitespace from string

I'm looking to remove some unwanted whitespace from a variable in place (ie without saving it to a file and such). Can someone show me a macro or technique that can do this?

I presently have

    <macrodef name="trim">
            <attribute name="property"/>
            <sequential>
                    <propertyregex
                            property="@{property}"
                            input="${@{property}}"
                            regexp="[\s]*(.+)[\s]*"
                            replace="\1"
                            override="true"
                    />
            </sequential>
    </macrodef>

Which I call like so

<for list="@{files}" delimiter="," param="val">
    <trim property="@{val}"/>

However this does not do the job. Any suggestions?

Upvotes: 1

Views: 1501

Answers (1)

Chad Nouis
Chad Nouis

Reputation: 7041

The <for> task has a trim attribute that will do exactly what you want:

If true, any leading or trailing whitespace will be removed from the list item before it is passed to the sequential.

An example:

<for list="@{files}" delimiter="," param="val" trim="true">
  <sequential>
    <echo>trimmed val: _@{val}_</echo>
  </sequential>
</for>

Upvotes: 2

Related Questions