Here you can find the source of longestCommonPrefix(String[] strArray)
public static String longestCommonPrefix(String[] strArray)
//package com.java2s; //License from project: Open Source License public class Main { public static String longestCommonPrefix(String[] strArray) { if (strArray.length == 0) return ""; if (strArray.length == 1) return strArray[0]; int maxLength = Integer.MAX_VALUE; for (int i = 0; i < strArray.length; i++) { maxLength = Math.min(strArray[i].length(), maxLength); }/*from www.jav a 2 s. c o m*/ if (maxLength == 0) return ""; for (int i = 0; i < maxLength; i++) { for (int j = 0; j < strArray.length - 1; j++) { if (strArray[j].charAt(i) != strArray[j + 1].charAt(i)) { return strArray[j].substring(0, i); } } } return strArray[0].substring(0, maxLength); } }