本文介绍了Ant子串按位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要按长度从属性值中提取一个子字符串,例如:

I need to extract a substring from property value by length, f.e. :

<property name="prop1" value="nameBLABLABLA" />

我想获取价值

name

是否可以不使用 javascript 代码?

Is it possible without using javascript code ?

推荐答案

不使用 vanilla ant,您需要添加一些 Ant 插件,例如
Antcontrib(2006 年最新版本!)或 AntFlaka - 意味着你需要额外的 jars/libraries.
使用 jdk 内置 Javascript 引擎,它就像:

Not with vanilla ant, you would need to add some Ant addon like
Antcontrib (latest release 2006 !) or Ant Flaka - means you'll need additional jars/libraries.
With using the jdk builtin Javascript engine it's as easy as :

<project>

<!-- create a macrodef for reuse -->
<macrodef name="getsubstring">
 <attribute name="src"/>
 <attribute name="from"/>
 <attribute name="to"/>
 <attribute name="result"/>
 <sequential>
  <script language="javascript">
   project.setProperty(
    "@{result}", "@{src}".substring(@{from},@{to})
    );
  </script>
 </sequential>
</macrodef>

<property name="foo" value="nameBLABLABLA"/>

<getsubstring src="${foo}" from="0" to="4" result="foobar"/>

<echo> $${foobar} => ${foobar}</echo>

</project>

不需要额外的库.
创建了一个适用于一般字符串属性的宏定义.
JavaScript 引擎理解 Javascript 和 Java,您将获得对 Ant api 的完全访问权限.

No additional libraries needed.
Created a macrodef that works for properties respectively for strings in general.
The JavaScript engine understands Javascript and Java and you'll get full access to Ant api.

这篇关于Ant子串按位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 23:53