2014-12-02 191 views
-1
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 

但它不工作...

+0

你是什么意思,它不起作用?你是否建议'String.Contains'或'Not'的实现存在问题? 'String2'的价值是什么,我怀疑你的情况有所不同。 – Jodrell 2014-12-02 17:47:02

+0

您是否有意忽略从第一次调用'Contains()'? – Panzercrisis 2014-12-02 17:56:41

+0

此外,如果涉及到换行符,您可能会遇到'LF'与'CRLF'(或'LFCR')的问题。 – Panzercrisis 2014-12-02 17:58:19

回答

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或vanilla IndexOf都失败。 但随着的IndexOf你可以写这样的事情

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

这将迫使的IndexOf像他们一样的字符串处理“东西”和“东西”。

最后,你的问题还不清楚,如果你想检查丢失的文本只有第二个字符串或两个字符串,但知道IndexOf返回> = 0如果搜索字符串存在,那么它应该很简单,修改if满足您的需求。

+2

这有什么帮助?像“Contains”一样,但更复杂。 – Jodrell 2014-12-02 17:47:36

+0

[Contains is implemented](http://referencesource.microsoft.com/mscorlib/R/428c5c9954dea844.html)使用IndexOf并假设StringComparison.Ordinal,如果您需要使用IndexOf,则可以更改这些内容。所以我可以说,是的可能更复杂(主观),但肯定它更灵活(客观) – Steve 2014-12-02 18:08:55

+0

使用'IndexOf'更复杂,因为您需要测试结果与'-1'是否相等。然而,正如你已经修改你的问题所说的那样,'IndexOf'确实有接受'StringComparison'参数的重载。我没有看到主观性,但这可能是主观的。 – Jodrell 2014-12-03 09:20:45

4

没关系,我们有一个字符串,

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"序区分大小写的比较。除非另有规定,字符串比较是序号区分大小写。


有关如何做一个文化/箱/正常化敏感扩大答案包含操作用绳子,see my answer here

0
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] 

Not就在前面String.Contains(blah)加法肯定装置the String doesn't contain [blah]。我没有看到Not Contains有什么问题..

你的代码出了什么问题?或者你的变量和他们的实际内容有什么问题?还是你失去了跟踪变量的地方?上部小心/小写的值和变量名(即使它被称为VB不关心,避免外壳megamix)

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

0

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

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