gpt4 book ai didi

javascript - Ant JavaScript 相同的字符串比较给出错误的结果,但是字符比较对字符串中的每个字符给出正确的结果?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:35:48 24 4
gpt4 key购买 nike

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

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

. . .但是脚本中的字符串比较没有像我预期的那样工作。相同字符串的逐个字符比较计算结果为“真”。一对取反的“>”和“<”比较的 AND 计算结果为“true”。但简单地比较 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 不是很有经验)。

想法?

最佳答案

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

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

关于javascript - Ant JavaScript 相同的字符串比较给出错误的结果,但是字符比较对字符串中的每个字符给出正确的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10659838/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com