I have a multipart string like this:
String Y = "part1 part2 part3 part4"; // This is only an example value
I want to write a function that compares the complete string Y with another strin, X. (Normally I will compare it with a list.) If the strings are not equal, then part1 part2 part3 should be compared with X. If they are not equal, X should be compared with part1 part2, and then finally with just part1.
I can use split(" ") to break the string up. I don’t know the number of chunks in the string. How can I write this comparison method?
You can use an algorithm like this:
That’s only pseudocode, of course. It seemed like you had a general idea how to do each of these individual parts. If you need more guidance, just let me know.
EDIT:
Assuming your strings will always be space-delimited like they are in your example, you could do something like this:
I haven’t tested this, and it may not be the most efficient way to perform the split, but I think the algorithm is clear.