FreezedHD
FreezedHD

Reputation: 1

Java check if something was added to a string

I periodically check if a string which I get from a web service changed. This works just fine but if an old string is deleted from my method triggers, too. For Example:

//I get this at the beginning
"One,Two,Three"

//And at the next check I get this
"Two,Three"

So the String changed and my method returned true like it is supposed to do. But I only want to return true if e.g. "Four" is added to the string.

Can anyone give me a solution for this problem?

Thank you a lot,

Freezed

Upvotes: 0

Views: 344

Answers (4)

Joseph Ryle
Joseph Ryle

Reputation: 129

Why not just trigger when the length of the string increases? The question doesn't state that what is being added matters--only whether something is being added at all.

boolean result = false;

if(newString.length() > oldString.length()) {
    result = true;
    break;
}

return result;

EDIT: Based on further clarification, I understand that the length of the string is not the best indicator, since something can be removed and added at the same time, in which case OP wants true returned--even if length is shorter. Here's a solution that splits the strings into tokens, and then checks whether the last token of the old string occurs before the last token of the new string, because that means something was added after it:

boolean result = false;
String delim = ",";
String oldStringTokens[] = oldString.split(delim);
String newStringTokens[] = newString.split(delim);

for(int i = 0; i < newStringTokens.length; i++) {
    if(oldStringTokens[oldStringTokens.length-1].equals(newStringTokens[i])) {
        if(i < newStringTokens.length - 1) {
            result = true;
        }
    }
}

return result;

Upvotes: 0

Treyten Carey
Treyten Carey

Reputation: 661

Perhaps you could use split like so

public class MyClass {
    public static void main(String args[]) {
        String oldString = "This,Is,A,Test";
        String[] oldItems = oldString.split(",");

        String newString = "This,Is,A,New";
        String[] newItems = newString.split(",");

        // For each new item, check all old items
        for (String newItem: newItems)
        {
            Boolean foundItem = false;
            for (String oldItem: oldItems)
            {
                // Item was already in the old items
                if (newItem.equals(oldItem))
                {
                    foundItem = true;
                    break;
                }
            }
            // New item is not in the old list of items
            if (!foundItem)
            {
                System.out.println("New item added: " + newItem);
            }
        }
    }
}

Upvotes: 0

Shivendra Agarwal
Shivendra Agarwal

Reputation: 688

if (!oldstring.contains(newstring)))  
return true;

Upvotes: 1

NotAUser
NotAUser

Reputation: 1466

Something like

newString.contains(oldString) && !newString.equals(oldString)

Upvotes: 0

Related Questions