Foxtable(狐表)用户栏目专家坐堂 → [求助]字符串是否包含数组关键字


  共有2330人关注过本帖树形打印复制链接

主题:[求助]字符串是否包含数组关键字

帅哥哟,离线,有人找我吗?
天一生水
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:五尾狐 帖子:1140 积分:11255 威望:0 精华:0 注册:2017/9/26 16:30:00
[求助]字符串是否包含数组关键字  发帖心情 Post By:2019/7/27 11:32:00 [只看该作者]

字符串中的关键字“根据.........之规定”,根据个人习惯会有不同的表述,比如:

依照、之规定
依据、规定
依、规定
据、之规定
...

我把上述关键字的组合放入表A的单元格中,每一组合 为一行。
如果字符串包含单元格中的任意一个组合,就显示“true”。
请老师帮助修改一下代码。谢谢!

'''
Dim str As String = "根据《行政诉讼法》第三条第二款之规定"
Dim lst As New List(of String)
lst = Tables("表A").Current.DataRow.Lines("第一列")
For Each ls As String In lst
    Dim ss() As String = ls.Split("、")
    For i As Integer =  0 To ss.count - 1
        If str.Contains(ss(i)) Then
            msgbox(true)
            Exit For
        End If
    Next
Next


图片点击可在新窗口打开查看此主题相关图片如下:截屏图片.jpg
图片点击可在新窗口打开查看


 回到顶部
帅哥哟,离线,有人找我吗?
有点蓝
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:107813 积分:548416 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2019/7/27 11:47:00 [只看该作者]

'''
Dim str As String = "根据《行政诉讼法》第三条第二款之规定"
Dim lst As New List(of String)
lst = Tables("表A").Current.DataRow.Lines("第一列")
For Each ls As String In lst
    Dim ss() As String = ls.Split("、")
    Dim p As Boolean = True
    For i As Integer =  0 To ss.count - 1
        If str.Contains(ss(i)) = False Then
            p = False
            Exit For
        End If
    Next
    If p Then
        msgbox(true)
        Exit For
    End If
Next

 回到顶部
帅哥哟,离线,有人找我吗?
天一生水
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:五尾狐 帖子:1140 积分:11255 威望:0 精华:0 注册:2017/9/26 16:30:00
  发帖心情 Post By:2019/8/9 15:00:00 [只看该作者]

蓝老师好!

我参照上面的代码,定位Word文档中包含关键字组合的段落。为什么段落存在,弹出的序号不对,序数大了100多。

代码哪里不对,请老师指教!

谢谢!

 

'''
Dim dlg As new OpenFileDialog
dlg.Filter = "word|*.doc;*.docx"
If dlg.ShowDialog = DialogResult.OK Then
    Dim app As New MSWord.Application
    try
        Dim doc = app.Documents.Open(dlg.FileName)
        Dim count = Doc.Characters.Count
        Dim rng As MSWord.Range = Doc.Range()
        'msgbox(rng.Text)
        Dim str As String = rng.text
        Dim ary() = str.replace(chr(10), "").split(new Char() {chr(13),chr(11)})      '清除段落空行
       
        Dim i1 = -1         '预设原告开始位置
        Dim str1 As String = "原告报告|原告、报告、身份情况"    '原告开始位置关键字组合
       
        Dim ls As new List(Of String)
        For Each s As String In ary    '遍历段落
            If s.trim() > "" Then
                ls.add(s)    '将段落加入集合

                For Each s1 As String In str1.split("|")      '判断是否包含st1关键字
                    Dim ss1() As String = s1.Split("、")
                    Dim lst1 As New List(of String)           '关键字新集合
                    lst1.AddRange(ss1)
                    Dim p As Boolean = True
                    For i As Integer =  0 To lst1.count - 1
                        If s.Contains(lst1(i)) = False Then
                            p = False
                            Exit For
                        End If
                    Next
                    If p Then
                        ' msgbox(true)
                        i1 = ls.count-1
                        msgbox(i1)
                        Exit For
                    End If
                Next
               
            End If
        Next
        app.quit
    catch ex As exception
        msgbox(ex.message)
        app.quit
    End try
End If


 回到顶部
帅哥哟,离线,有人找我吗?
有点蓝
  4楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:107813 积分:548416 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2019/8/9 15:12:00 [只看该作者]

自行调试

For Each s As String In ary    '遍历段落
            If s.trim() > "" Then

output.show(ls.count & " = " & s)

                ls.add(s)    '将段落加入集合

                For Each s1 As String In str1.split("|")      '判断是否包含st1关键字

output.show("s1 = " & s1)

                    Dim ss1() As String = s1.Split("、")
                    Dim lst1 As New List(of String)           '关键字新集合
                    lst1.AddRange(ss1)
                    Dim p As Boolean = True
                    For i As Integer =  0 To lst1.count - 1
                        If s.Contains(lst1(i)) = False Then

output.show("包含 = " & lst1(i))
                            p = False‘表示找到,这里p=false
                            Exit For
                        End If
                    Next

output.show("p = " & p)
                    If p Then 'p=true,这里表示的是没有找到
                        ' msgbox(true)
                        i1 = ls.count-1
                        msgbox(i1)
                        Exit For
                    End If
                Next
                
            End If
        Next


 回到顶部