java 蚂蚁字符串函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3725827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ant string functions?
提问by Mike Q
Does Ant have any way of doing string uppercase/lowercase/captialize/uncaptialize string manipulations? I looked at PropertyRegex but I don't believe the last two are possible with that. Is that anything else?
Ant 有没有办法对字符串进行大写/小写/大写/取消大写的字符串操作?我查看了 PropertyRegex,但我不相信最后两个是可能的。那是别的吗?
回答by JoseK
From this thread, use an Ant <script>
task:
从这个线程,使用 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>
Output
输出
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
Updatefor WarrenFaith's comment to separate the script into another target and pass a property from the called targetback to the calling target
更新WarrenFaith 的评论以将脚本分离到另一个目标并将属性从被调用目标传递回调用目标
Use antcallbackfrom the ant-contrib jar
使用来自 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>
and capitalise
task uses the passed in param1
thus
和capitalise
任务使用传入的param1
因此
<target name="capitalize">
<property name="foo" value="${param1}"/>
Final output
最终输出
[echo] a = THIS IS A NORMAL LINE THAT DOESN'T SAY MUCH