Reputation: 1613
I was trying to figure out a way to compare two strings and returns their 'common' words, given that the strings are always in lowercase, I wanted to create a function for this..for example
str1 = "this is a test"
str2 = "saldkasl test asdasd"
result = stringcompare(str1, str2) 'returns "test"
the common word between the two strings should be "test" and if, the two strings have two or more common words, the function should concatenate the strings
str1 = "this is another test"
str2 = "another asdsada test asdsa"
result = stringcompare(str1, str2) ' returns "another test"
i have found a useful link, it gave me an idea but somehow something is really lacking
A pseudocode of what I'm doing right now is this,
**
'1st: separate the words by every space, " ", then store it in an array or list
'2nd: compare each item on the list, if equal then store to variable 'result'
**
is this okay? I think it is slow and maybe there is someone out there that has a better approach on this..thanks
Upvotes: 1
Views: 1937
Reputation: 26424
As measured in VS 2013, below solution is on average 20% faster than Guffa's:
Dim str1 As String = "this is another test"
Dim str2 As String = "another asdsada test asdsa"
Dim result As String = String.Join(" ", str1.Split(" "c).
Intersect(str2.Split(" "c)))
Results were obtained by looping each solution 100000 times and measuring time with StopWatch.
Upvotes: 3
Reputation: 700302
Use a hash set for the words in the first string, then you can just loop through the words in the second string and check if they exist in the first, and get close to O(n) performance:
Dim first As New HashSet(Of String)(str1.Split(" "c))
Dim result As String() = str2.Split(" "c).Where(Function(s) first.Contains(s)).ToArray()
Upvotes: 1