VB.NET-向.contains添加多个字符串 [英] VB.NET - Adding more than 1 string to .contains

查看:471
本文介绍了VB.NET-向.contains添加多个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTMLElementCollection,我将使用For Each循环来查看InnerHTML是否包含某些单词。如果它们确实包含这些关键字中的任何一个,它将保存到文件中。

I have an HTMLElementCollection that I'm going through using a For Each Loop to see if the InnerHTML contains certain words. If they do contain any of those keywords it gets saved into a file.

一切正常,但我想知道是否有一种简化方法。这是一个示例

Everything works fine but I was wondering if there is a way to simplify. Here's a sample

For Each Helement As HtmlElement In elements

     If Helement.InnerHtml.Contains("keyword1") Or Helement.InnerHtml.Contains("keyword2") Or Helement.InnerHtml.Contains("keyword3") Or Helement.InnerHtml.Contains("keyword4") Or Helement.InnerHtml.Contains("keyword5") = True Then
         ' THE CODE TO COPY TO FILE
     End If

Next Helement

是否存在任何可以正常工作的东西:

Does anything exist that would work like:

If Helement.InnerHtml.Contains("keyword1", "keyword2", "keyword3", "keyword4", "keyword5")

我的工作方式

推荐答案

您可以编写一个扩展方法到提供多输入选项的字符串,例如:

You could write an Extension Method to string that provides a multi-input option, such as:

 Public Module StringExtensionMethods
     Private Sub New()
     End Sub
     <System.Runtime.CompilerServices.Extension> _
     Public Function Contains(ByVal str As String, ByVal ParamArray values As String()) As Boolean
         For Each value In values
             If str.Contains(value) Then
                 Return True
             End If
         Next
         Return False
     End Function
 End Module

然后您可以调用它,如第二个示例:)

You could then call that instead, as in your second example :)

这篇关于VB.NET-向.contains添加多个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆