如果Statement Actionscript 3缩短

时间:2016-04-11 04:38:00

标签: actionscript-3 flash if-statement

我刚刚开始学习动作脚本,我想知道如何缩短这个if语句

if (txtInputTemp.charAt(track - 1) == "a" || txtInputTemp.charAt(track - 1) == "b" || txtInputTemp.charAt(track - 1) == "c" || txtInputTemp.charAt(track - 1) == "d" || txtInputTemp.charAt(track - 1) == "e" || txtInputTemp.charAt(track - 1) == "f" || txtInputTemp.charAt(track - 1) == "g" || txtInputTemp.charAt(track - 1) == "h" || txtInputTemp.charAt(track - 1) == "i" || txtInputTemp.charAt(track - 1) == "j" || txtInputTemp.charAt(track - 1) == "k" || txtInputTemp.charAt(track - 1) == "l" || txtInputTemp.charAt(track - 1) == "m" || txtInputTemp.charAt(track - 1) == "n" || txtInputTemp.charAt(track - 1) == "o" || txtInputTemp.charAt(track - 1) == "p" || txtInputTemp.charAt(track - 1) == "q" || txtInputTemp.charAt(track - 1) == "r" || txtInputTemp.charAt(track - 1) == "s" || txtInputTemp.charAt(track - 1) == "t" || txtInputTemp.charAt(track - 1) == "u" || txtInputTemp.charAt(track - 1) == "v" || txtInputTemp.charAt(track - 1) == "w" || txtInputTemp.charAt(track - 1) == "x" || txtInputTemp.charAt(track - 1) == "y" || txtInputTemp.charAt(track - 1) == "z" || txtInputTemp.charAt(track - 1) == "." || txtInputTemp.charAt(track - 1) == "'" || txtInputTemp.charAt(track - 1) == "-" || txtInputTemp.charAt(track - 1) == " ") { }

感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

它可以将a-z重写为范围规范。

somechar>="a" && somechar<="z"

但我不知道如何缩短这些符号。

== "."
== "'"
== "-"
== " "


var somechar:String = txtInputTemp.charAt(track - 1);
if ((somechar>="a" && somechar<="z")||somechar == "."||somechar == "'"||somechar == "-"||somechar == " ")
{
    // do something
}

工作示例:http://wonderfl.net/c/A9Hv

选项2:使用array.indexOf(char) == -1。

var someArray: Array = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",".","'","-"," "];

var somechar:String = txtInputTemp.charAt(track - 1);

if (someArray.indexOf(somechar) != -1)
{
    // Do something
}