Example usage for org.apache.commons.lang StringUtils indexOfDifference

List of usage examples for org.apache.commons.lang StringUtils indexOfDifference

Introduction

In this page you can find the example usage for org.apache.commons.lang StringUtils indexOfDifference.

Prototype

public static int indexOfDifference(String[] strs) 

Source Link

Document

Compares all Strings in an array and returns the index at which the Strings begin to differ.

Usage

From source file:org.overlord.sramp.shell.TabCompleter.java

/**
 * Merge candidates.//from w  ww.  java  2s . c  o  m
 *
 * @param completionCandidates
 *            the completion candidates
 * @param buffer
 *            the buffer
 * @return the string
 */
private String mergeCandidates(List<String> completionCandidates, String buffer) {
    if (completionCandidates.size() > 1) {
        int indexOfDifference = StringUtils
                .indexOfDifference(completionCandidates.toArray(new String[completionCandidates.size()]));
        if (indexOfDifference == -1) {
            return completionCandidates.get(0);
        } else {
            String partToCompare = ""; //$NON-NLS-1$
            String commonPart = completionCandidates.get(0).substring(0, indexOfDifference);
            if (commonPart.startsWith(buffer)) {
                partToCompare = buffer;
            } else {
                partToCompare = buffer.substring(buffer.lastIndexOf(" ") + 1); //$NON-NLS-1$
            }
            if (partToCompare.length() != indexOfDifference) {
                return commonPart;
            }

        }
    }
    return ""; //$NON-NLS-1$

}