Reputation: 7228
I have list of situation like below: string A <-------> string B
I should compare A with B with following condition:
1- if number shown in read is different in both side but the rest is the same it means A = B.
2- there is in some situation like the first one in A side after number shown in red there is not white space but in b side there is a white space after number shown in red and also after X.
3- there is also different cases like number 3
Now how can i best compare this two string?
private static void controlSimilarity(String memo,String ck,String bc,String id,String product) {
if(!id.equals(product)){
listIdentifier.add(new MmoCnBcIdProduct(memo,ck,bc,id,product));
}
Upvotes: 1
Views: 613
Reputation: 13872
After Sean owen's suggestion; probably following Regex
can be used:
/(\w.*?)(\d+)X(\w.*)/
and capture group 1 and 3 should be equal after dropping any space in-between.
Spilt
the strings by space .
Compare [0]
and [1]
of splitted results.
Combine all remaining indexes to make a single string. (use trim
before combining)
find indexOf
X
and use subString
till end of string.
Use equals
OR equalsIgnoreCase
to compare the combined (sub)strings.
Repeat from step 1, for each set of strings.
Upvotes: 0
Reputation: 26796
Based on your sample data, I advise normalizing every string by adding space between every group of digits and characaters. So sample 2
TASIGNA CAPS 1X200MG <----> TASIGNA CAPS 112 X 200 MG
would become:
TASIGNA CAPS 1 X 200 MG <----> TASIGNA CAPS 112 X 200 MG
Now just split on whitespace and compare the single groups. All should be the same, but one numerical group. Depending on the type of your data (CAPS, COMP, CREME, ...) you may ignore another group. Either the one before the X, or the one after CREME, etc. This strongly depends on your data.
Upvotes: 0
Reputation: 12212
I'd try to "normalize" strings. Make both uppercase, replace "\s+X\s+" with "X", replace "\s+%" with "%", "\s+MG" with "MG" etc., then split it by whitespaces or some regex (Scanner class or Guava's Splitter) and compare parts of the string.
Upvotes: 1