Dj_Mike238
Dj_Mike238

Reputation: 65

Check if a string does NOT contain some text

If String1.Contains("something") AndAlso String2.'does not contain something'
    'do stuff
End If

Is there a simple way to do this? I've tried with

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

But it doesn't work...

Upvotes: 1

Views: 63718

Answers (4)

Jodrell
Jodrell

Reputation: 35716

okay, we have a string,

Dim someString = "this something that"

the expression

someString.Contains("something")

evaluates to True. The expression

someString.Contains("something else")

evaluates to False. The expression

Not someString.Contains("something else")

evaluates to True.


Note:

The expression

someString.Contains("Something")

evaluates to False becuase "Something" fails an Ordinal Case-Sensitive comparison with "something". Unless specified otherwise, string comparisons are Ordinal Case-Sensitive.

Upvotes: 10

Nek
Nek

Reputation: 241

You can use vbNullString to check if the string contains a text or not. Try this code:

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

Upvotes: -1

Steve
Steve

Reputation: 216293

Just use IndexOf on the two strings and then use the if expression

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 returns the character position of the string passed as argument. If the argument is not found in the source string the return is -1 as explained in the MSDN docs

IndexOf could also be useful if your search term contains characters in different case than the input string.
For example, if your input text contains the word "Something" (Uppercase 'S') searching this input using the term "something" will fail both using Contains or the vanilla IndexOf. But with IndexOf you could write something like this

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

This will force IndexOf to treat "Something" and "something" like they are the same string.

Finally, your question is unclear if you want to check for the missing text only the second string or both strings, but knowing that IndexOf returns >= 0 if the searched string exists then it should be pretty simple to modify the if condition to suit your needs.

Upvotes: 4

Karl Stephen
Karl Stephen

Reputation: 1140

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

The above code outputs :

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

The addition of Not right in front of String.Contains(blah) definately means the String doesn't contain [blah]. I don't see anything wrong with Not Contains..

What's wrong with your code ? Or what's wrong with your variables and their actual content ? Or have you lost track of your variables somwhere ? Carefull of upper/lower cases in values AND variables names (even if it's known VB doesn't care, avoid casing megamix)

Use a Breakpoint just before your If condition test, then run the debugger. Have a look at String1 and String2 contents.

Upvotes: 0

Related Questions