find Longer Common Prefix between two string - Android java.lang

Android examples for java.lang:String Algorithm

Description

find Longer Common Prefix between two string

Demo Code

public class Main {

  static String findLongerCommonPrefix(String name2, String candidate) {
    if (name2 == null) {
      return candidate;
    } else {//from   w  ww . j av a2 s  .co m
      boolean isEqual = true;
      int i = 0;
      for (; isEqual && i < Math.min(name2.length(), candidate.length()); i++) {
        isEqual = name2.charAt(i) == candidate.charAt(i);
        if (!isEqual) {
          i--;
        }
      }
      return name2.substring(0, i);
    }
  }

}

Related Tutorials