I have a query regarding the matching of two alphanumeric values. Suppose like i have a first alphanumeric value say 185,Bakers Street,London and the same second one like 185BakersStreetLondon (without special characters) . how we can match these in apex code?
It's not ideal but you could use getLevenshteinDistance to figure out how close the two Strings are. See the example below:
String s = '185,Bakers Street,London'; System.debug(s.getLevenshteinDistance(s)); // Outputs 0 System.debug(s.getLevenshteinDistance('185BakersStreetLondon')); // Outputs 3 System.debug(s.getLevenshteinDistance('123 Fake Avenue, Manchester')); // Outputs 22
It depends on how strict you want to be with what you consider to be a match. For example, should the comparison be case-sensitive? But if you just want to remove special characters and then compare the 2 strings, something like this should work.
String myStringWithoutSpecial = '185BakersStreetLondon'; String myStringWithSpecial = '185,Bakers Street,London'; String charactersToRemove = '[^a-zA-Z0-9]'; //Remove anything that's not a letter or number String myStringCleaned = myStringWithSpecial.replaceAll(charactersToRemove,''); System.assertEquals(myStringWithoutSpecial, myStringCleaned);