2016-11-15 145 views
0

Hy!比較ANT中的兩個字符串

我必須比較ANT中的兩個字符串與<equals>任務,但即使控制檯顯示正確的字符串,它也不適用於我的代碼。這是代碼:

<echo file="file.txt" append="true">AB${line.separator}</echo> 
<echo file="file.txt" append="true">CD${line.separator}</echo> 

<loadfile property="content" srcFile="file.txt"> 
    <filterchain> 
     <filterreader classname="org.apache.tools.ant.filters.HeadFilter"> 
     <param name="lines" value="1" /> 
     </filterreader> 
    </filterchain> 
</loadfile> 
<echo>${content}</echo> 

      <if> 
       <equals arg1="${content}" arg2="AB" /> 
        <then><echo message="OK"/></then> 
        <else><echo message="not OK"/></else> 
      </if> 

構建的結果是:

11:24:23  [echo] AB 
11:24:23  [echo] not OK 

你能告訴我怎麼解決這個問題嗎?和thk :) :)

+0

請注意,「if」任務不是標準的ANT功能,它是第三方ant-contrib擴展的一部分。 –

回答

1

我懷疑arg2屬性是缺少行分隔符。試試這個:

<equals arg1="${content}" arg2="AB${line.separator}" /> 

或者,甚至更好,嘗試清潔所有行分隔符的content

+0

是的!就是這樣。謝謝 – Carlinto