Reputation: 7
I am having a string of the form as
PosAttributes: FN,CT,ST;
Now when i compute some functionality i get a string as
PosAttributes1: FN,ST,CT;
Now though both the strings suggest the same thing and if use following equal function it returns false. I know both stings are not same, but the semantics are same. What should i do?
PosAttributes.equals(PosAttributes);
Upvotes: 1
Views: 5701
Reputation: 1265
I have the same requirement at work right now and wanted to avoid using lists for the evaluation.
What I did was to check if the two string to compare are of equal length - this means that it is possible that they might be the same, just with different order.
Now remove one by one comma-separated string in the main string, found in the compare string. If the output of the main string is empty, that means the two are an exact math. Please see the pseudo-code below (I did not paste the actual code because it has some company specific info):
private static boolean isStringCombinationEqual(final String main, final String compare)
{
boolean result = false;
String modifiedMain = main;
// if not the same length, then cannot possibly be same combination in different order
if (main.length() == compare.length())
{
final String[] compareArr = // split compare using delimiter
for (int i = 0; i < compareArr.length; i++)
{
if (i > 0)
{
modifiedMain = // replace delimiter with empty string
}
modifiedMain = // replace compareArr[0] with empty string
}
if (//modifiedMain is empty)
{
result = true;
}
}
return result;
}
Upvotes: 0
Reputation: 3
You can either override the equal method or sort both string then compare them.
Upvotes: 0
Reputation: 347204
So assuming that the example text is the full text, you need to remove the ;
character, as this would change the contents of the String
, split the String
on the ,
character, sort the resulting arrays and compare them, something like...
String[] v1 = "FN,CT,ST;".replace(";", "").split(",");
String[] v2 = "FN,ST,CT;".replace(";", "").split(",");
Arrays.sort(v1);
Arrays.sort(v2);
System.out.println(Arrays.equals(v1, v2));
Which outputs true
What I might be tempted to do is write a method which returned a sorted array of the String
, replicating all the common functionality...
public static String[] sorted(String value) {
String[] v1 = value.replace(";", "").split(",");
Arrays.sort(v1);
return v1;
}
And then you could simply call it, which would allow you to do a single like comparison...
System.out.println(Arrays.equals(sorted("FN,CT,ST;"), sorted("FN,ST,CT;")));
The next step might be to write a method which returns true
, which called sorted
and Arrays.equals
to make it easier...
System.out.println(isEqual("FN,CT,ST;", "FN,ST,CT;"));
But, I'll leave that up to you ;)
Upvotes: 0
Reputation: 79838
You need to break out the individual sections of each String, and store them in some kind of Set - that's a structure where either there's no order, or where the order doesn't affect the outcome of the equals
method. I'd write a method like this.
private static Set<String> attributeSet(String input) {
String[] attributes = input.split(",");
return new HashSet<String>(Arrays.asList(attributes));
}
This breaks a String into its segments, assuming the separator is a comma. Then it uses a standard trick to convert the resulting array into a HashSet
, which is a commonly used type of Set
.
Then when I want to compare two strings, I could write something like
if (attributeSet(string1).equals(attributeSet(string2))) {
// ...
}
Upvotes: 0
Reputation: 44834
As the string is delimitered by commas you could use a String.split to give
String arr[] = PosAttributes.split (",");
String arr2[] = PosAttributes1.split (",");
then you just need to loop through the first arrays ensuring that ALL elements are in the second array. Also check that the sizes are identical.
Upvotes: 1