解析字符串中的布尔值与Function.apply使用 [英] Parse boolean values in strings for use with Function.apply

查看:122
本文介绍了解析字符串中的布尔值与Function.apply使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 String.split 来解析命令行字符串转换成字符串数组。然后将结果用于调用使用 Function.apply API函数。

I'm using String.split to parse a command line string into an array of strings. The result is then used to call a function using the Function.apply API.

如果申请(NULL,[17])被调用这个函数:

static function test(foo:int):void
{
    trace(foo, typeof(foo));
}

它按预期工作(输出: 17号)。

但是,调用申请(NULL,[假])申请(NULL,[0])具有这种功能:

However, calling apply(null, ["false"]) or apply(null, ["0"]) with this function:

static function test(foo:Boolean):void
{
    trace(foo, typeof(foo));
}

不工作(预期输出:假布尔;实际输出:真布尔)。

does not work (expected output: false Boolean; actual output: true Boolean).

有没有一种方法,使之认识到真实(或其他任何东西)作为布尔值,就像它与数字串?理想情况下真实也应该保持有效的字符串值。

Is there a way to make it recognize "true" and "false" (or anything else) as Boolean values, just like it does with numerical strings? Ideally "true" and "false" should also remain valid string values.

推荐答案

没有,没有国米preT字符串内置的方式真与假的布尔值。

No, there is no built in way to interpret the strings "true" and "false" as booleans.

在ActionScript和其他ECMA脚本语言中,被评估为布尔值false唯一的字符串值是一个空字符串。任何字符串的长度被评价为真正的布尔EX pression,包括字符串假和0。

In ActionScript, and other ECMA script languages, the only string value that gets evaluated as boolean false is an empty string, "". Any string with a length gets evaluated as true in a boolean expression, including the strings "false" and "0".

在实践中,这不是一个大问题,因为它是很容易做一个字符串比较。一堆在这里建议:<一href="http://stackoverflow.com/questions/263965/how-can-i-convert-a-string-to-boolean-in-javascript">http://stackoverflow.com/questions/263965/how-can-i-convert-a-string-to-boolean-in-javascript

In practice, this is seldom a big problem, since it is quite easy to make a string comparison. A bunch of suggestions here: http://stackoverflow.com/questions/263965/how-can-i-convert-a-string-to-boolean-in-javascript

这篇关于解析字符串中的布尔值与Function.apply使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆