2012-05-18 43 views
1

我在我的project.xml中使用JavaScript - 通过<scriptdef> - 从多个属性值组成属性名称。我需要比较脚本中的两个字符串:一个是作为一个或多个属性值的扩展传入的。另一个作为引用的文字字符串传入。 。 。Ant JavaScript相同的字符串比较给出了错误的结果,但char比较给出了字符串中每个字符的真值?

<ac:var name="buildVariant" unset="true"/> 
<property name="buildVariant" value="Debug"/> 
<unStrung propstring="${buildVariant}" altifmatch="Debug"/> 

。 。 。但是脚本中的字符串比较并不像我期望的那样工作。相同字符串的逐字符比较评估为“真”。 AND>一对否定“>”和“<”比较评估为“真”。但简单地比较string1 == string2的计算结果为“false”。这是一个简化的脚本,说明了这个问题(并且显示了我尝试过的一些解决方法)。 。 。

<scriptdef name="unStrung" language="javascript"> 
    <attribute name="propstring"/> 
    <attribute name="altifmatch"/> 
<![CDATA[ 
var propstring = attributes.get("propstring"); 
var propvalue = project.getProperty(propstring); 
var altifmatch = attributes.get("altifmatch"); 
var debugTheDebug = "Debug"; 

if (altifmatch != null && propstring != null) 
{ 
    var alen = 0; 
    var plen = 0; 
    alen = altifmatch.length(); 
    plen = propstring.length(); 

    print(' '); 
    print(' altifmatch = [' + altifmatch + '] and propstring = [' + propstring + ']'); 
    print(' so naturally (altifmatch == propstring) = [' + (altifmatch == propstring) + '],'); 
    print(' just like ("Debug" == "Debug") = [' + ("Debug" == "Debug") + '].'); 
    print(' '); 

    print(' '); 
    print(' altifmatch.length() = [' + alen + '] and propstring.length() = [' + plen + ']'); 
    print(' altifmatch.substring(0,alen) = [' + altifmatch.substring(0,alen) 
     + '] and propstring.substring(0,plen) = [' + altifmatch.substring(0,alen) + ']'); 
    print(' so naturally (propstring.substring(0,plen) == propstring.substring(0,plen)) = [' 
       + (altifmatch.substring(0,alen) == propstring.substring(0,plen)) + '].'); 
    print(' '); 

    for (var c=0; c<plen; c++) 
    { 
     print(' char['+c+']: altifmatch['+c+']="'+altifmatch.charCodeAt(c)+'"; propstring['+c+']="'+propstring.charCodeAt(c) 
      +'". So ... a == p = "' + (altifmatch.charCodeAt(c) == propstring.charCodeAt(c)) + '"'); 
    } 

    print(' '); 
    print(' typeof(altifmatch) = "' + typeof(altifmatch) + '", and typeof(propstring) = "' + typeof(propstring) + '"'); 
    print(' altifmatch.toString() = "' + altifmatch.toString() + '" and propstring.toString() = "' + propstring.toString() + '"'); 
    print(' ...oddly enough... debugTheDebug = "' + debugTheDebug + '"'); 
    print('  (debugTheDebug == altifmatch.toString()) = "' + (debugTheDebug == altifmatch.toString()) + '"'); 
    print('  (debugTheDebug == propstring.toString()) = "' + (debugTheDebug == propstring.toString()) + '"'); 
    print(' ...and still... (altifmatch.toString() == propstring.toString()) = "' + (altifmatch.toString() == propstring.toString()) + '"'); 

    print(' '); 
    print('  (debugTheDebug == altifmatch) = "' + (debugTheDebug == altifmatch) + '"'); 
    print('  (debugTheDebug == propstring) = "' + (debugTheDebug == propstring) + '"'); 
    print(' ...and still... (altifmatch == propstring) = "' + (altifmatch == propstring) + '"'); 
    print('  (altifmatch < propstring) = "' + (altifmatch < propstring) + '"'); 
    print('  (altifmatch > propstring) = "' + (altifmatch > propstring) + '"'); 
    print('   (!(altifmatch < propstring) && !(altifmatch > propstring)) = "' 
      + (!(altifmatch < propstring) && !(altifmatch > propstring)) + '"'); 
    print(' ...and of course... ((debugTheDebug == altifmatch) && (debugTheDebug == propstring)) = "' 
      + ((debugTheDebug == altifmatch) && (debugTheDebug == propstring)) + '"'); 

    print(' '); 
} 
]]> 
</scriptdef> 

输出结果是这样的:

altifmatch = [Debug] and propstring = [Debug] 
so naturally (altifmatch == propstring) = [false], 
just like ("Debug" == "Debug") = [true]. 


altifmatch.length() = [5] and propstring.length() = [5] 
altifmatch.substring(0,alen) = [Debug] and propstring.substring(0,plen) = [Debug] 
so naturally (propstring.substring(0,plen) == propstring.substring(0,plen)) = [false]. 

char[0]: altifmatch[0]="68"; propstring[0]="68". So ... a == p = "true" 
char[1]: altifmatch[1]="101"; propstring[1]="101". So ... a == p = "true" 
char[2]: altifmatch[2]="98"; propstring[2]="98". So ... a == p = "true" 
char[3]: altifmatch[3]="117"; propstring[3]="117". So ... a == p = "true" 
char[4]: altifmatch[4]="103"; propstring[4]="103". So ... a == p = "true" 

typeof(altifmatch) = "object", and typeof(propstring) = "object" 
altifmatch.toString() = "Debug" and propstring.toString() = "Debug" 
...oddly enough... debugTheDebug = "Debug" 
    (debugTheDebug == altifmatch.toString()) = "true" 
    (debugTheDebug == propstring.toString()) = "true" 
...and still... (altifmatch.toString() == propstring.toString()) = "false" 

    (debugTheDebug == altifmatch) = "true" 
    (debugTheDebug == propstring) = "true" 
...and still... (altifmatch == propstring) = "false" 
    (altifmatch < propstring) = "false" 
    (altifmatch > propstring) = "false" 
     (!(altifmatch < propstring) && !(altifmatch > propstring)) = "true" 
...and of course... ((debugTheDebug == altifmatch) && (debugTheDebug == propstring)) = "true" 

我怀疑这是一些简单的或愚蠢的,我错过了(我不是很有经验的蚂蚁或JavaScript)。

想法?

+0

见,也:相关的和有趣的,但不是直接回答我的问题。 。 。 [为什么有两种JavaScript字符串?](http://stackoverflow.com/questions/5514367/why-are-there-two-kinds-of-javascript-strings) – Kenigmatic

回答

1

为字符串比较,你应该使用(字符串).equals()

http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Object.html

+0

这很好用!我不明白的是为什么我没有找到这个选项与很多搜索。 。 。 (好吧,也许是因为我不熟悉JavaScript),但是我发现很多命令说==应该工作(只要没有尾随空白或隐藏的字符)。 谢谢!另外,我会为您的答案投票,但我没有足够的代表,但! – Kenigmatic

+0

那么我承认我不是一个蚂蚁亲 - 但据我了解 - 蚂蚁是一个Java构建工具。即使您正在使用''设置/指令 - 据我了解,它只是简单地调用一些内在机制来“将脚本处理为java本身” - 意思就是说,正如我所理解的那样 - Ant java引擎“使java可以工作从脚本指令“--- TL:DR /酷的故事兄弟 - 基本上一个”==“比较将在纯JavaScript中滑动 - 但是你要做的是要求ant从js构建java指令。因为这种“==”由于比较的性质而失败 - 很高兴它的工作和可以帮助! –

+0

我认为必须破坏某些东西,因为'=='的许多实例能够正常工作,而那些无法正常工作的实例显得不合理,尤其是在与“正常”情况混合使用时。 。 。 (a == b)= true,(a == c)= true,(b == c)= false' 。 。 。和。 。 。 (a> b)= false,(a b)&&!(a Kenigmatic