0

我想阻止用户使用网络摄像头模拟器,我在AS2中通过使用 senocular 的功能做到了这一点,但我不能让它在 AS3 中工作,所以,这是senocular 的旧版本,我也想做同样的事情在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");
}

谢谢您的帮助 !

4

2 回答 2

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;
}
于 2011-04-15T17:57:29.867 回答
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;
}
于 2011-04-15T16:18:16.677 回答