2

I am trying to develop a new salesforce app where I am having some XML and Apex changes. That's why I wanted to know if you guys know any way to compare XML or Apex code within Apex. To be clear, I wanted to compare an Apex Class that was updated. So I'd like to have all the Difference beetwen that apex class. I have both Strings. String A (New Code) and String B (Old Code)

Note: I've used String.difference(String); but it returns just the first difference and so on.

Do you know how to accomplish this in Force.com or if you have any Secure API to invoke from apex passing String A and String B or even if you have used any Algorithm to extract differences.

3
  • I think you're going to have to roll your own.
    – Adrian Larson
    Commented Mar 6, 2017 at 18:58
  • Yes! I'm trying it, but that's why I aksed if there are something in Force.com available to use, or maybe any External API to use it.
    – mauriD
    Commented Mar 6, 2017 at 18:59
  • This may help get you started: stackoverflow.com/questions/29573700/…
    – Eric
    Commented Mar 6, 2017 at 19:26

1 Answer 1

0

I came up with this, could be enhanced for sure.

 /**
   * Returns the characters which are different between two strings
   * @param  a First string
   * @param  b Second string
   * @return   A string with the characters which are different
   */
  public static String getDifference(String a, String b) {
    Integer i = 0;
    Integer j = 0;
    List<Integer> chars = new List<Integer>();
    String res = '';

    while (j < b.length()) {
      if (a.charAt(i) != b.charAt(j) || i == a.length())
        chars.add(b.charAt(j));
      else
        i++;
      j++;
    }
    return String.fromCharArray(chars);
  }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .