Jess
Jess

Reputation: 227

Groovy String Comparison

I need to know if two strings "match" where "matching" basically means that there is significant overlap between the two strings. For example, if string1 is "foo" and string2 is "foobar", this should be a match. If string2 was "barfoo", that should also be a match with string1. However, if string2 was "fobar", this should not be a match. I'm struggling to find a clever way to do this. Do I need to split the strings into lists of characters first or is there a way to do this kind of comparison already in Groovy? Thanks!

Upvotes: 0

Views: 7148

Answers (2)

tim_yates
tim_yates

Reputation: 171194

Using Apache commons StringUtils:

@Grab( 'org.apache.commons:commons-lang3:3.1' )
import static org.apache.commons.lang3.StringUtils.getLevenshteinDistance

int a = getLevenshteinDistance( 'The quick fox jumped', 'The fox jumped' )
int b = getLevenshteinDistance( 'The fox jumped', 'The fox' )

// Assert a is more similar than b
assert a < b

Levenshtein Distance tells you the number of characters that have to change for one string to become another

So to get from 'The quick fox jumped' to 'The fox jumped', you need to delete 6 chars (so it has a score of 6)

And to get from 'The fox jumped' to 'The fox', you need to delete 7 chars.

Upvotes: 4

Will
Will

Reputation: 14559

As per your examples, plain old String.contains may suffice:

assert 'foobar'.contains('foo')
assert 'barfoo'.contains('foo')
assert !'fobar'.contains('foo')

Upvotes: 1

Related Questions