【问题标题】:Find String that starts with in array AS3查找以数组 AS3 开头的字符串
【发布时间】:2011-08-06 11:15:02
【问题描述】:

我想阻止用户使用网络摄像头模拟器,我在 AS2 中通过使用 senocular 的功能做到了这一点,但我无法使其在 AS3 中工作,所以,这里是 old version by 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");
}

感谢您的帮助!

【问题讨论】:

    标签: arrays flash actionscript-3 actionscript indexof


    【解决方案1】:

    好的,我将之前的答案留给历史。现在我已经明白你想要什么了:

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 您好 Kodiak,感谢您的回复,但它不能按我的需要工作,如果我在“ok”中添加一些内容,例如:“ok v2.4”,那么它不会跟踪任何内容.
      • 您需要更精确地定义您需要的内容,这里我的函数返回完全以给定参数开头的字符串...
      猜你喜欢
      • 2022-08-11
      • 2013-12-14
      • 1970-01-01
      • 1970-01-01
      • 2022-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-02
      相关资源
      最近更新 更多