Here you can find the source of longestCommonPrefix(String one, String two)
Parameter | Description |
---|---|
one | The one String |
two | The other String |
public static String longestCommonPrefix(String one, String two)
//package com.java2s; //License from project: Open Source License public class Main { /**/*from w w w.j a va 2 s .com*/ * Returns the longest common prefix for two Strings or <tt>""</tt> if no prefix is shared * * @param one The one String * @param two The other String * * @return The longest common prefix, or "" */ public static String longestCommonPrefix(String one, String two) { if (one.equals(two)) { return one; } if (one.equals("") || two.equals("")) { return ""; } String currentPrefix = ""; int size = 0; while (one.startsWith(currentPrefix) && two.startsWith(currentPrefix)) { size++; currentPrefix = one.substring(0, size); } return one.substring(0, size - 1); } }