Ant JavaScript相同的字符串比较给出了错误的结果,但是char compare对于字符串中的每个char都给出了true?

时间:2012-05-18 21:14:49

标签: javascript ant ivy string-comparison

我在我的project.xml中使用JavaScript - 通过<scriptdef> - 从多个属性值组成属性名称。我需要比较我的脚本中的两个字符串:一个作为一个或多个属性值的扩展传入。另一个作为带引号的文字字符串传入。 。

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

。 。 。但是脚本中的字符串比较并没有像我预期的那样工作。相同字符串的逐字符比较评估为“true”。一对否定的“&gt;”的AND和“&lt;”比较评估为“真实”。但只是将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"

我怀疑我错过了一些简单或愚蠢的东西(我对Ant或JavaScript不是很有经验)。

想法?

1 个答案:

答案 0 :(得分:1)

对于字符串比较,您应该使用(String).equals()

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