Hello, i was wondering how would i make a search kind of thing, for example if string1 contains all the words in string2 words. like a search(not just if string1 contains string 2).

it should work something like this:

If string1 is "an apple" and if string2 is "djsjfsfg apple sdfsfssdfs" then string3 would be nothing

BUT

If string1 is "an apple" and if string2 is "djsjfsfg apple sdfsfssdfs an ashdfjdsgfsdgfj" then string3 would be "djsjfsfg apple sdfsfssdfs an ashdfjdsgfsdgfj"

Hi,
Break your search string into an array of words e.g. an apple would become an array with two items {"an", "apple"}. Check your second string for each item in the array by looping through it and if it is true in every case you have a match.

for(int i = 0; i<array.length;i++) {
   if(string2.contains(array[i]) {
      boolean == true
   } else {
      boolean == false
   }
}

If the boolean value every becomes false you don't have a match.
Hope that helps,

You mean that all the letters from string1 are in string2?
Is so, hericles solution will work, but partly, you need a break point of the for loop, when all indexes are reached.

hericles`s code needs to be modified a bit (and in VB):

Dim s1 As String = "an apple"
Dim s2 As String = "each apple is not an issue"

Dim s1Array As String() = s1.Split(" "C)
Dim bContains As Boolean = False
For i As Integer = 0 To s1Array.Length - 1
	If s2.Contains(s1Array(i)) Then
		bContains = True
	Else
		Exit For
	End If
Next
If bContains Then
	Console.WriteLine("All words that are in s1 are in s2 as well.")
Else
	Console.WriteLine("s2 does NOT contain all words from s1.")
End If

Edited 5 Years Ago by Mitja Bonca: n/a

no, I want hericles function but in vb.net.

but thanks for the reply's you two.

To do it by an exact word per word basis you need to split both strings.

Version1

Dim String1 As String = "an apple"
Dim String2 As String = "each apple is not an issue"

Dim AllString1Words As String() = String1.Split(" "c)
Dim AllString2Words As String() = String2.Split(" "c)

Dim String2ContainsAllString1Words As Boolean = True

For Each String1Word As String In AllString1Words
     If Not AllString2Words.Contains(String1Word) Then
          String2ContainsAllString1Words = False
          Exit For
     End If
Next

If String2ContainsAllString1Words Then
     MsgBox("All words that are in String1 are in String2.")
Else
     MsgBox("String2 does NOT contain all words from String1.")
End If

Version 2

Dim String1 As String = "an apple"
Dim AllWordsInString2 As String() = "each apple is not an issue".Split(" "c)

If Array.TrueForAll(String1.Split(" "c), Function(word) AllWordsInString2.Contains(word)) Then
    MsgBox("All words that are in String1 are in String2.")
Else
    MsgBox("String2 does NOT contain all words from String1.")
End If

To do it by an exact word per word basis you need to split both strings.

Version1

Dim String1 As String = "an apple"
Dim String2 As String = "each apple is not an issue"

Dim AllString1Words As String() = String1.Split(" "c)
Dim AllString2Words As String() = String2.Split(" "c)

Dim String2ContainsAllString1Words As Boolean = True

For Each String1Word As String In AllString1Words
     If Not AllString2Words.Contains(String1Word) Then
          String2ContainsAllString1Words = False
          Exit For
     End If
Next

If String2ContainsAllString1Words Then
     MsgBox("All words that are in String1 are in String2.")
Else
     MsgBox("String2 does NOT contain all words from String1.")
End If

Version 2

Dim String1 As String = "an apple"
Dim AllWordsInString2 As String() = "each apple is not an issue".Split(" "c)

If Array.TrueForAll(String1.Split(" "c), Function(word) AllWordsInString2.Contains(word)) Then
    MsgBox("All words that are in String1 are in String2.")
Else
    MsgBox("String2 does NOT contain all words from String1.")
End If

wow! thanks man! you're the best!

This question has already been answered. Start a new discussion instead.