以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  [求助]indexof查找字符失败  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=128329)

--  作者:山中树人100
--  发布时间:2018/12/4 8:52:00
--  [求助]indexof查找字符失败
使用以下代码,窗体显示-1,表示查找不到,为什么?

Dim wtms As String =“消防主机瘫痪(有推进记录,扣分减半)” 
If wtms.contains("扣") = True Then
Dim ls1 As Integer = wtms.indexof("扣")
messagebox.show(wtms.indexof(ls1))
End If

--  作者:有点甜
--  发布时间:2018/12/4 8:55:00
--  
Dim wtms As String ="消防主机瘫痪(有推进记录,扣分减半)"
If wtms.contains("扣") = True Then
    Dim ls1 As Integer = wtms.indexof("扣")
    messagebox.show(ls1)
End If

--  作者:dixiaxiaodan
--  发布时间:2019/4/2 22:36:00
--  
谢谢有点甜老师了,我想通过在导入前就判断下,是否已经导入过一次,代码修改如下:
Dim web As New System.Windows.Forms.WebBrowser()
Dim Cols() As String = { "指令编号","修正案号","标题","生效日期" }
Dim dr As DataRow
For pg As Integer = e.Form.Controls("textbox1").Value To e.Form.Controls("textbox2").Value
\'Dim pg As Integer = 1
    Dim wz As String = "http://www.caacaad.org.cn/index.php?a=2&t=1&page= "& pg & "&"
    web.Navigate( wz )
    Do Until web.ReadyState = 4
        Application.DoEvents
    Loop

    Dim elems As object = web.Document.GetElementById("table1").GetElementsByTagName("tr")
    Dim m As Integer = 0
    Dim r As Integer = 0
    For Each elem As object In elems
        r = r + 1
        If r = elems.count Then
            Exit For
        End If
        Dim i As Integer = -1
        m = m + 1
        Dim tdelems As object =  elem.GetElementsByTagName("td")
        Dim adnum As String = tdelems(0).Innertext 
        If m > 1 Then  \'因为网页的表格有标题,如果不判断,就会增加出一个空行
            If tdelems(0).Innertext Like "*A320*" Or tdelems(0).Innertext Like "*A330*" Or tdelems(0).Innertext Like "*MULT*" Or tdelems(0).Innertext Like "*B737*" Then
                dr = DataTables("表A").Find("指令编号 = zl ")
                If dr Is Nothing Then
                    Tables("表A").AddNew
                    For Each tdelem As object In tdelems
                        i = i+1
                        Tables("表A").Current(Cols(i)) = tdelems(i).Innertext
                        If i > 2 Then
                            Tables("表A").Current(Cols(i)) = tdelems(i+2).Innertext
                            Exit For
                        End If
                    Next
                End If
            End If
        End If
    Next 
Next
Tables("表A").save
但是会报错,还得您指点下。

图片点击可在新窗口打开查看此主题相关图片如下:微信截图_20190402223529.png
图片点击可在新窗口打开查看


--  作者:有点蓝
--  发布时间:2019/4/2 23:11:00
--  
1、GetElementsByTagName返回的是一个数组,需要判断2个东西,这个数组是否是空对象,这个数组的个数是否大于0。所有使用到这个函数的地方都需要判断

Dim tdelems As object =  elem.GetElementsByTagName("td")
if tdelems isnot  nothing andalso tdelems.length > 0 ..........


2、Tables("表A").Current(Cols(i)) = tdelems(i).Innertext
网站的td个数和Cols数组的个数并不是一一对应的,不能使用i来进行索引,建议还是老老实实使用笨办法赋值

 If dr Is Nothing Then
   dim r as row = Tables("表A").AddNew
r("指令编号") = tdelems(0).Innertext
r("修正案号") = tdelems(1).Innertext
……