【问题标题】:TextField.text is assigned with a multi-line string, but they're not equal right after the assignmentTextField.text 被分配了一个多行字符串,但它们在分配后不相等
【发布时间】:2017-01-26 05:18:28
【问题描述】:

我有一个字符串数组:

private var phrase:Array = ["You will be given a series of questions like this:\n2 + 2 =\n(click or press ENTER to continue)","You can use the Keyboard or Mouse\nto deliver the answer\n\"ENTER\" locks it in.\n(click or press ENTER to continue)","\nClick Here\n to start."];  

我在脚本后面有一个条件来查看phrase[0] 是否等于instructText.text,所以我在赋值之后直接放了一个“测试”,如下所示:

instructText.text = phrase[0];
if (instructText.text == phrase[0]) {
    trace("phrase zero");
}
else {
    trace("nottttttttt");
}  

//OUTPUT: nottttttttt

我尝试了phrase[0] as String 和String(phrase[0]) 的各种组合,但都没有成功。

我错过了什么?

【问题讨论】:

  • 在if/else 语句之前,你为什么不直接trace("phrase 0 is : " + String(instructText.text) );,它看起来像什么?不相等/不准确?

标签: string actionscript-3 if-statement textfield multilinestring


【解决方案1】:

原来TextField 类的text 属性转换了“换行”字符("\n"、ASCII code 的 1010=A16) 转为“回车”字符(ASCII 码 1310=D16)。

因此,您需要进行 LF 到 CR 的转换(或反之亦然),以便将属性中存储的内容与数组元素中的内容进行同类比较:

function replaceLFwithCR(s:String):String {
    return s.replace(/\n/g, String.fromCharCode(13));
}
if (instructText.text == replaceCRwithLF(phrase[0])) {
    trace("They are equal :)");
}
else {
    trace("They are NOT equal :(");
}
// Output: They are equal :)


附:要获取字符的代码,您可以使用String 类的charCodeAt() 方法:
trace("\n".charCodeAt(0)); // 10

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-07
    • 1970-01-01
    • 2023-01-09
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    • 2011-12-21
    相关资源
    最近更新 更多