2011-04-15 191 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