0

I need to change the order of my string to the Expected value. And also the ',' should get match according to my expected.

String actual = "10684 ANNA MARIE DR, GLEN ALLEN, VA, APT 111, 23060";
String expected = "10684 ANNA MARIE DR, APT 111, GLEN ALLEN, VA 23060";

So that, I can assert the result like this

Assert.assertEquals(actual,expected);

Any help would be greatly appreciated.

5
  • 3
    Is this a single case, or must it be a general function? Commented Mar 22, 2017 at 10:38
  • @LppEdd - It is a generic method, where i can pass the actual and expected via its arguments Commented Mar 22, 2017 at 10:40
  • What if an element occurs twice in the original? Commented Mar 22, 2017 at 10:40
  • @Aishu What's the generic order of elements? (street, city, state etc) And how do you recognize them? Will the input strings be always like that? Commented Mar 22, 2017 at 10:43
  • 1
    Are the actual strings always in the same order? Or can they have any order? And do you want to rearrange them just to be able to compare them or is it enough to actually know whether they do contain the same information? For two out of three of those cases, you already have answers available. Commented Mar 22, 2017 at 10:47

6 Answers 6

1

Split the strings based on ,, sort the resulting arrays, then use Assert.assertArrayEquals to compare the arrays

Sign up to request clarification or add additional context in comments.

Comments

1

You can split them and sort the array and then check whether they're equal or not.

String actual = "10684 ANNA MARIE DR, GLEN ALLEN, VA, APT 111, 23060";
String expected = "10684 ANNA MARIE DR, APT 111, GLEN ALLEN, VA 23060";

String arr1[] = actual.split(", ");
String arr2[] = expected.split(", ");

Arrays.sort(arr1);
Arrays.sort(arr2);

Assert.assertArrayEquals( arr1, arr2 );

5 Comments

You are assuming the OP need to do an alphabetical sort on his string.
Good solution, assuming that OPs goal really just is to assert if both strings contain the same information. If his primary goal is to actually rearrange the strings, however... - we still need more clarification from OP.
From what I got from the question is that OP is getting a string in which all the comma (,) separated elements should appear in expected string. The order is not maintained. This answer deals with that case.
@domdom you're right. However, "rearrange the strings" is a business logic and I hardly anticipate such a thing would be required in a test case. So as long as he clarifies, my answer would be this.
I agree, that's why yours is my favorite answer here so far.
1

You can always do it in that a little stupid way:

String actual = "10684 ANNA MARIE DR, GLEN ALLEN, VA, APT 111, 23060";
String[] arr = actual.split(", ");
String result = arr[0] + ", " + arr[3] + ", " + arr[1] + ", " + arr[2] + " " + arr[4];

1 Comment

If the actual strings are always in the same order, then yes, this would be the straight forward solution. But OP didn't really clarify that yet.
0

You should replace that String with Address class and then easily compare addresses. In Effective Java book you can find chapter where they say that String is not good to use to replace some other structure(and obviously you have address structure here).

Comments

0

Unlike others suggested, that is splitting by ,, arrays aren't going to be equal.

VA and 23060 will result in two different elements, while in the expected String they are a single element.

A clarification from OP is needed.

Comments

0
String actualArray[] = actual.split(",");
int i = 0;

String finalStr = actualArray[i] +","+actualArray[i+3]+","+actualArray[i+1]+","+actualArray[i+2]+actualArray[i+4];
System.out.println(finalStr);

1 Comment

Isn't that the very same as Мотя answer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.