Here you can find the source of longestCommonPrefix(String str1, String str2)
public static String longestCommonPrefix(String str1, String str2)
//package com.java2s; //License from project: Apache License public class Main { public static String longestCommonPrefix(String[] strs) { if (strs.length == 1) { return strs[0]; }//ww w .j a v a2 s . com String str = longestCommonPrefix(strs[0], strs[1]); int i = 2; while (i < strs.length) { str = longestCommonPrefix(str, strs[i]); i++; } return str; } public static String longestCommonPrefix(String str1, String str2) { int i = 0; while (i < str1.length() && i < str2.length() && str1.charAt(i) == str2.charAt(i)) { i++; } String str = str1.substring(0, i); return str; } }