2011-04-15 139 views
0

我想阻止用户使用网络摄像头模拟器,我通过使用senocular的函数在AS2中完成了这项工作,但是我无法使其在AS3中工作,所以,这里是old version by senocular,我想在AS3,尝试使用indexOf,但不起作用,我需要找到至少字符串的前4个字符,并将它们与AS3中的数组内的项目进行比较!查找以字符串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

好的,我留下我以前的答案为历史。现在,我知道你想要什么:

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

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

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; 
} 
+0

科迪亚克您好,感谢您的答复,但我需要它,如果我添加的东西“OK”像它不起作用:“OK V2.4 “那么它不会追查任何东西。 – Alex 2011-04-15 16:33:29

+0

你需要更精确地定义你需要的东西,这里我的函数返回的字符串以给定的参数开始...... – Kodiak 2011-04-15 17:23:09