4

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

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

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

<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 不是很有经验)。

想法?

4

1 回答 1

1

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

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

于 2012-05-18T21:31:25.843 回答