Java tutorial
//package com.java2s; public class Main { /** * @param s1 * @param s2 * @return the common string in both {@code s1} and {@code s2} */ public static String getCommonString(String s1, String s2) { int l1 = s1.length(); int l2 = s2.length(); if (l1 == l2) { return s1; } int length = Math.min(l1, l2); String returnString = null; for (int i = 0; i < length; i++) { if (s1.charAt(i) == s2.charAt(i)) { continue; } returnString = s1.substring(0, i); break; } if (returnString == null) { returnString = l1 < l2 ? s1 : s2; } if (returnString.endsWith("/")) { int l = returnString.length(); if (l > 1) { return returnString.substring(0, l - 1); } } return returnString; } }