Ant字符串函数?

时间:2010-09-16 10:38:51

标签: java string ant

Ant有没有办法做字符串大写/小写/ captialize / uncaptialize字符串操作?我查看了PropertyRegex,但我不相信最后两个是可能的。还有别的吗?

2 个答案:

答案 0 :(得分:14)

From this thread,使用Ant <script>任务:

<target name="capitalize">
    <property name="foo" value="This is a normal line that doesn't say much"/>

    <!-- Using Javascript functions to convert the string -->
    <script language="javascript"> <![CDATA[

        // getting the value
        sentence = project.getProperty("foo");

        // convert to uppercase
        lowercaseValue = sentence.toLowerCase();
        uppercaseValue = sentence.toUpperCase();

        // store the result in a new property
        project.setProperty("allLowerCase",lowercaseValue);
        project.setProperty("allUpperCase",uppercaseValue);

    ]]> </script>

    <!-- Display the values -->
    <echo>allLowerCase=${allLowerCase}</echo>
    <echo>allUpperCase=${allUpperCase}</echo>
</target>

输出

D:\ant-1.8.0RC1\bin>ant capitalize
Buildfile: D:\ant-1.8.0RC1\bin\build.xml

capitalize:
     [echo] allLowerCase=this is a normal line that doesn't say much
     [echo] allUpperCase=THIS IS A NORMAL LINE THAT DOESN'T SAY MUCH

BUILD SUCCESSFUL

更新以获取WarrenFaith的评论,将脚本分成另一个目标,并将属性从调用目标 传递回调用目标

使用ant-contrib jar中的antcallback

<target name="testCallback">
    <antcallback target="capitalize" return="allUpperCase">
        <param name="param1" value="This is a normal line that doesn't say much"/>
    </antcallback>
    <echo>a = ${allUpperCase}</echo>
</target>

capitalise任务使用传入的param1因此

 <target name="capitalize">

        <property name="foo" value="${param1}"/>

最终输出

   [echo] a = THIS IS A NORMAL LINE THAT DOESN'T SAY MUCH

答案 1 :(得分:0)

您可以使用script任务并使用jsr223支持的脚本语言(如javascript,jruby,jython,...)进行字符串处理