【问题标题】:Check if a string does NOT contain some text检查字符串是否不包含某些文本
【发布时间】:2015-01-31 01:41:38
【问题描述】:
If String1.Contains("something") AndAlso String2.'does not contain something'
    'do stuff
End If

有没有简单的方法来做到这一点?我试过了

If String1.Contains("something") AndAlso Not String2.Contains("something else")
    'do stuff
End If

但它不起作用......

【问题讨论】:

  • 什么意思,它不起作用?你认为String.ContainsNot的实现有问题吗? String2的值是多少,我怀疑你的大小写有区别。
  • 你是否有意在第一次调用Contains()时省略Not
  • 此外,如果涉及换行符,您可能会遇到LFCRLF(或LFCR)的问题。
  • 你能举一个不起作用的字符串的例子和预期的结果吗?

标签: vb.net string text contains


【解决方案1】:

好的,我们有一个字符串,

Dim someString = "this something that"

表达式

someString.Contains("something")

计算为True。表达式

someString.Contains("something else")

计算为False。表达式

Not someString.Contains("something else")

计算为True


注意:

表达式

someString.Contains("Something")

评估为False,因为"Something""something" 的序数区分大小写比较失败。除非另有说明,否则字符串比较是区分大小写的。

【讨论】:

    【解决方案2】:

    只需对两个字符串使用 IndexOf,然后使用 if 表达式

    Dim pos1 = String1.IndexOf("something") 
    Dim pos2 = String2.IndexOf("something else")
    
    if pos1 < 0 AndAlso pos2 < 0 then
        ' the string1 doesn't contain "something" and string2 doesn't contain "something else"
    End If
    

    string.IndexOf 返回作为参数传递的字符串的字符位置。如果在源字符串中找不到参数,则返回 -1,如 MSDN docs 中所述

    如果您的搜索词包含与输入字符串不同大小写的字符,IndexOf 也很有用。
    例如,如果您的输入文本包含单词“Something”(大写“S”),使用术语“something”搜索此输入时,使用 Contains 或普通 IndexOf 都将失败。 但是使用 IndexOf 你可以写这样的东西

    Dim pos1 = String1.IndexOf("something", StringComparison.CurrentCultureIgnoreCase) 
    Dim pos2 = String2.IndexOf("something else", StringComparison.CurrentCultureIgnoreCase)
    

    这将强制 IndexOf 将“Something”和“something”视为同一个字符串。

    最后,您的问题不清楚是否要仅检查第二个字符串或两个字符串是否缺少文本,但是如果搜索到的字符串存在,则知道 IndexOf 返回 >= 0 那么修改 if 条件应该很简单以满足您的需求。

    【讨论】:

    • 这有什么帮助?像Contains,但更复杂。
    • Contains is implemented 使用 IndexOf 并假设 StringComparison.Ordinal,如果您需要使用 IndexOf 可以更改它。所以我可以说,是的,也许更复杂(主观),但肯定更灵活(客观)
    • 使用IndexOf 更复杂,因为您需要测试结果是否与-1 相等。但是,正如您修改了您的问题所说的那样,IndexOf 确实有接受 StringComparison 参数的重载。我认为没有主观性,但这可能是主观的。
    【解决方案3】:
    Dim String1 As String = "This one contains something at index 18"
    Dim String2 As String = "You won't find your string here !"
    
    Debug.WriteLine("String1 : IndexOf(something) = " _
        + String1.IndexOf("something").ToString())
    Debug.WriteLine("String2 : IndexOf(something else) = " _
        + String1.IndexOf("something else").ToString())
    
    If String1.Contains("something") AndAlso Not String2.Contains("something else") Then
        Debug.WriteLine("String1 contains [something]")
        Debug.WriteLine("String2 doesn't contain [something else]")
    End If
    

    以上代码输出:

    'String1 : IndexOf(something) = 18
    'String2 : IndexOf(something else) = -1
    'String1 contains [something]
    'String2 doesn't contain [something else]
    

    String.Contains(blah)前面加上Not肯定意味着the String doesn't contain [blah]。我看不出Not Contains.. 有什么问题。

    您的代码有什么问题?或者您的变量及其实际内容有什么问题?或者您是否在某个地方忘记了您的变量?小心值和变量名称中的大写/小写(即使已知 VB 不在乎,避免使用 megamix)

    If 条件测试之前使用 Breakpoint,然后运行 调试器。查看 String1String2 内容。

    【讨论】:

      【解决方案4】:

      您可以使用 vbNullString 来检查字符串是否包含文本。试试这个代码:

      If String1.Text = vbNullString AND String2.Text = vbNullString Then
      'do stuff
      else
      'the string is empty
      End if
      

      【讨论】:

        猜你喜欢
        • 2016-08-09
        • 2018-05-01
        • 2014-05-07
        • 2011-03-31
        • 2015-03-15
        • 2019-12-29
        • 1970-01-01
        • 2020-04-15
        相关资源
        最近更新 更多