2013-11-29 63 views
0

下面是我的数组和代码,用于检查文本框中的数据是否在数组内。问题是当我运行程序时,数组的值总是“”或者没有找到数据。我的代码出了什么问题?请帮助我..谢谢。检查文本框的值是否在数组内

 a = Split((a), vbTab) 
    devices = Array("iPhone5", "iPhone4", "iPhone3", "iPad", "iPod", "iPhone4s", "iPhone3G", "iPhone3gs", "gt-s5360", "gt-i9505", "n7100", "gt-n7100", "i9300", "gt-i9300", "gt-p3100", "s5300", "gt-s5300", "gt-s7562", _ 
      "gt-i8190", "s100", "p5100", "gt-p5100", "gt-s6102", "gt-i9100", "gt-p3110", "gt-p6200", "n8000", "gt-n8000", "gt-i9082", "sm-t210", "gt-n7105", "n7000", "gt-n7000", "gt-n5100", "GT-S5570", "GT-S5830i", _ 
      "GT-S5830", "GT-I8262", "GT-P1000", "Nexus 7", "GT-I8160", "H120", "ALCATEL ONE TOUCH 918N", "HuaweiG510-0200", "MyPhone A919 Duo", "MyPhone A848i Duo", "C6603", "ALCATEL ONE TOUCH 4030E", "LG-E400", _ 
      "GT-P6800", "ICE 350e", "GT-I9070", "ALCATEL ONE TOUCH 5021E", "Cherry w500", "GT-I8150", "LT22i", "Spark TV", "I9500", "GT-I9500", "Burst S280", "W120", "GT-P7500", "MyPhone A888 Duo", "GT-S5301", "Thunder S220", "GT-S7500", _ 
      "GT-I8552", "SM-T211", "GT-S5282", "A818 Duo", "LT26i", "GT-S6802", "GT-S5570I", "HuaweiY210-0100", "LT26w", "HTC One", "ST23i", "ST27i", "SHW-M250S", "Cruize W280", "Titan TV S320", "B1-A71", "GT-I9152", "W110", "7038", _ 
      "LT18i", "GT-P3113", "GT-I9000", "Cherry Sonic", "GT-S5670", "SHW-M110S", "ST26i", "SonyEricssonMT25i", "Excite_352g", "LT25i", "Lenovo A390_ROW", "ST25i", "LG-E612", "GT-I9003") 
    urls = Array("youtube.com", "ytimg.com", "DoubleClick.net", "google.com", "fbcdn.net", "google -analytics.com", "yimg.com", "googlesyndication.com", "facebook.com", "gstatic.com", "mywebacceleration.com", "yahoo.com", "scorecardresearch.com", _ 
      "google.com.ph", "adnxs.com", "redtubefiles.com", "rubiconproject.com", "wattpad.net", "www.com", "youjizz.com", "bing.net", "akamaihd.net", "xvideos.com", "tumblr.com", "twitter.com", "yieldmanager.com", "sharethis.com", "wikimedia.org", _ 
      "y8.com", "sulitstatic.com", "globe.com.ph", "googleapis.com", "tagstat.com", "quantserve.com", "addthis.com", "blogspot.com", "king.com", "cloudfront.net", "ayosdito.com", "ask.com", "openx.net", "bigspeedpro.com", "gravatar.com", _ 
      "amasvc.com", "bing.com", "cdn.com", "yldmgrimg.net", "cedexis.com") 



For intX = 0 To UBound(a) 
     If Text11.Text = "" Then 
      a(intX) = UCase(a(intX)) 
      Text11.Text = a(intX) 
     ElseIf Text12.Text = "" Then 
      a(intX) = UCase(a(intX)) 
      Text12.Text = a(intX) 
      If Len(Text12.Text) <= 17 Then 
       Text12.Text = "" 
      Else 
       b = Split(Text12.Text, "/") 
       For i = 0 To UBound(b) 
        Text12.Text = b(2) 
       Next 
       Text12 = InStr(Text12, urls) 
        If Text12 = UCase(urls) Then 
         Text22 = Text12 
         Text25 = count + 1 
        Else 
         Text26 = Text12 
         Text27 = othercount + 1 
        End If 
      End If 
+2

当我将urls变成urls()..我得到一个错误“Expected Array”..我该怎么办?这是我第一次在vb6中使用数组..有人请帮助我..谢谢。 – bebebe

+0

您的代码似乎没有问题,但第一行除外。你是否在某个地方标注了变量'a'?看起来你正在尝试分割一个字符串,并将其重新分配给相同的变量。在你的代码中添加一个'Option Explicit'语句作为第一行,定义一个字符串数组变量,并尝试为其分配'Split(a,vbTab)'。 – jac

+2

是的,我已经提到了变量a并使用split解析它,我也有选项显式在我的代码顶部。我唯一的问题是数组.. – bebebe

回答

1

您可以使用ForEach使其更有效。 或使用此功能... :)

 
Public Function IsContained(theArray() As Variant, strSearchPharse As String, Optional IsMatch As Boolean = False, Optional IsCaseSensitive As Boolean = True) As Boolean 
On Error Resume Next 
If Not UBound(theArray) 0 Then End 
Dim strExploded As String 
Dim gsChache As Boolean 
gsChache = False 'set the default value 
'Checking for every array thing 
    For Each strExploded In theArray 
    If IsMatch And Not (Len(strSearchPharse) = Len(strExploded)) Then 'if its matchable... 
      If IsCaseSensitive And strExploded = strSearchPharse Then gsChache = True 
      If Not IsCaseSensitive And LCase(strExploded) = LCase(strSearchPharse) Then gsChache = True 
    ElseIf Not IsMatch And Not IsCaseSensitive Then 'if its not matchable, and not case sensitive 
     If InStr(0, LCase(strExploded), LCase(strSearchPharse)) >= 1 Then gsChache = True 
    Else 'if its not matchable, and Case Sensitive 
     If InStr(0, strExploded, strSearchPharse) >= 1 Then gsChache = True 
    End If 
    DoEvents 
    Next strExploded 

IsContained = gsChache 'finish 
End Function 

如何使用它?
theArray是用于数组变量的,strSearchPharse是要搜索的文本(如关键字,例如apple或youtube),然后isMatch是您要搜索的文本需要完全相同(不区分大小写,您启用它。),IsCaseSensitive是您需要搜索的文本需要区分大小写。
默认设置为“不匹配”和“区分大小写”。


样品:

 
XYZ = Array("Satu", "Dua", "Tiga", "Empat", "Lima", "Enam", "Tujuh", "Delapan", "Sembilan", "Sepuluh") 
If IsContained(XYZ, "o", , False) Then 
    MsgBox "The Array contains o alphabet." 
Else 
    MsgBox "The Array have no o alphabet." 
End If 

结果将显示 “数组包含Ø字母。” MsgBox ... :)

+2

hi @Gunawan Christianto ..你能给我一个关于数组的运行代码吗? – bebebe

+0

嗯...像什么? :) –