-1

Is it possible, and if how, to get the changed part of a string?

Let's say I'm constantly checking a string for a change BUT when it does change the change is somewhere randomly in the string and never in the same place. How would i find out what changed and store that in a separate string?

String Original = "Random Text"; 
String Changed = "ra7ndom Text"
String Output = "7";

public void getChange() {
// ??
}

I need to get what was added to the string, after it was modified.
  • Give example to understand it better.. – Ninad Pingale Jun 20 '14 at 10:06
  • 4
    Welcome to StackOverflow! Do you have any code snippet to provide, so that we can better understand your issue? What have you tried already? – sp00m Jun 20 '14 at 10:06
  • how do you check, what have you done so far, where does the change come from? have you read the [help center for questions](http://stackoverflow.com/help/how-to-ask), what Java resources have you read up on so far? – avalancha Jun 20 '14 at 10:07
  • I have understood absolutely nothing from your description. Give sample input and output. – TheLostMind Jun 20 '14 at 10:10
  • I'm checking a HTML URL(a forum) constantly(every 20 seconds) I am trying to get the added Messages, So far i just iterate over all characters on the page. and every time i check the java program freezes for a second or too so I'm looking for another method. I store the data on a String, and on close in a file. – user3759644 Jun 20 '14 at 10:11
  • How about checking the `last modified` or similar header? And I wouldn't treat it as a String. Getting the last object of a certain part of a DOM tree is much easier. – Markus W Mahlberg Jun 20 '14 at 10:29

4 Answers4

1

My solution is this:

public int getFirstChangeIndex(CharSequence original, CharSequence changed)
{
    int n = original.length();
    for (int i = 0; i < n; i++)
    {
        if (original.charAt(i) != changed.charAt(i))
            break;
        n++;
    }
}
Majid Laissi
  • 19,188
  • 19
  • 68
  • 105
ikh
  • 10,119
  • 1
  • 31
  • 70
0

There does not exist a simple way/method to do this for you. You could write a method, which loops through both Strings, comparing each character and returning the differences in a string.

Checkout this question: What is the easiest/best/most correct way to iterate through the characters of a string in Java?

Or you could use an external library, which has more advanced functionality. A good one is the Google-diff-match-patch

Community
  • 1
  • 1
Erwin
  • 3,298
  • 2
  • 15
  • 22
0

Another solution would be to use Apache Commons StringUtils' difference method.

Markus W Mahlberg
  • 19,711
  • 6
  • 65
  • 89
0

No quick and easy way exists, you'll need to compare each individual character in the Strings. Note that Strings themselves never change, they are immutable, but you can compare different Strings.

Here's something I knocked up which will tell you there the first difference is, feel free to adapt it for your needs:

String oldString = "abcdexghijklmn";
String newString = "abcdefghijklm";

//get length of longer string
int end = Math.max(oldString.length(), newString.length());

//loop through strings
for (int position=0; position<end; position++) {

  if (position>=newString.length()) {

    // Reached the end of new string
    System.out.println("Difference at position "+position+" new string is shorter");
    break;

  } else if (position>=oldString.length()) {

    // Reached the end of old string
    System.out.println("Difference at position "+position+" new string is longer");
    break;

  } else {

    //compare characters at this position       
    char newChar = newString.charAt(position);
    char oldChar = oldString.charAt(position);
    if (newChar!=oldChar) {
      System.out.println("Difference at position "+position);
      break;
    }

  }

}
NickJ
  • 9,380
  • 9
  • 51
  • 74