954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Search string for words

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"

AndyPants
Light Poster
35 posts since Oct 2011
Reputation Points: 15
Solved Threads: 1
 

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,

hericles
Practically a Posting Shark
823 posts since Nov 2007
Reputation Points: 136
Solved Threads: 167
 

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
Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
 

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

but thanks for the reply's you two.

AndyPants
Light Poster
35 posts since Oct 2011
Reputation Points: 15
Solved Threads: 1
 


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
Unhnd_Exception
Posting Pro
571 posts since Nov 2010
Reputation Points: 249
Solved Threads: 201
 

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!

AndyPants
Light Poster
35 posts since Oct 2011
Reputation Points: 15
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: