查找以数组AS3开头的字符串

时间:2011-04-15 15:58:48

标签: arrays flash actionscript-3 actionscript indexof

我想阻止用户使用网络摄像头模拟器,我已经通过使用senocular的功能在 AS2 中完成了这项工作,但是我无法在AS3中使用它,因此,这里是{{3我希望在 AS3 中做同样的事情,尝试使用 indexOf 但是不起作用,我需要找到字符串的前4个字符并将它们与AS3中数组内的项目进行比较!

String.prototype.startsWith = function(str){
        return !this.indexOf(str);
    }

这是我想要做的事情:

var bannedDevices = new Array("FakeCam","SplitCam","Phillips Capture Card 7xx","VLC");

var myDeviceName = "SplitCam v1.5";  //"Splitcam" in bannedDevices should trigger this;

if (myDeviceName.indexOf(bannedDevices)){
   trace("banned device");
}

感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

好的,我留下以前的历史回答。现在我已经理解了你想要的东西:

public function FlashTest() {
    var bannedDevices:Array = new Array("FakeCam","SplitCam","Phillips Capture Card 7xx","VLC");

    var myDeviceName:String = "SplitCam v1.5";  //"Splitcam" in bannedDevices should trigger this;

    trace(startsWith(myDeviceName, bannedDevices, 4));
}

/**
* @returns An array of strings in pHayStack beginning with pLength first characters of pNeedle
*/
private function startsWith(pNeedle:String, pHayStack:Array, pLength:uint):Array
{
    var result:Array = [];
    for each (var hay:String in pHayStack)
    {
        if (hay.match("^"+pNeedle.substr(0,pLength)))
        {
            result.push(hay);
        }
    }
    return result;
}

答案 1 :(得分:1)

您的需求不是很清楚......这是一个函数,它返回以给定字符串开头的数组中的每个字符串。

public function FlashTest() {
    var hayStack:Array = ["not this one", "still not this one", "ok this one is good", "a trap ok", "okgood too"];

    trace(startsWith("ok", hayStack));
}

/**
* @returns An array of strings in pHayStack beginning with the given string
*/
private function startsWith(pNeedle:String, pHayStack:Array):Array
{
    var result:Array = [];
    for each (var hay:String in pHayStack)
    {
        if (hay.match("^"+pNeedle))
        {
            result.push(hay);
        }
    }
    return result;
}