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



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

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

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

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

想法?

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

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

最新更新