Java String Abbreviate abbreviateDotSeparatedString(String string, int targetLength)

Here you can find the source of abbreviateDotSeparatedString(String string, int targetLength)

Description

Abbreviates the given dot-separated string to reduce its length (if necessary) to within the supplied target length.

License

Open Source License

Parameter

Parameter Description
string The String to abbreviate
targetLength The target length for the abbreviated String

Return

The abbreviated String.

Declaration

public static String abbreviateDotSeparatedString(String string, int targetLength) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2008, 2010 VMware Inc./*from   w  w  w. j  av a 2  s .com*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   VMware Inc. - initial contribution
 *******************************************************************************/

public class Main {
    private static final String DOT_SEPARATOR = ".";
    private static final String DOT_SEPARATOR_SPLIT_REGEX = "\\.";

    /**
     * Abbreviates the given dot-separated string to reduce its length (if necessary) to within the supplied target
     * length.
     * 
     * <p />
     * 
     * The abbreviation is performed by, starting with the left-most segment, replacing each segment in turn with its first
     * character until the length is equal to or less than the target. If this approach fails to sufficiently reduce the
     * string's length, the right-most segment is returned in its entirety, even if its length exceeds the target
     * length.
     * 
     * <p />
     * 
     * For example with the input alpha.bravo.charlie.Delta the following results are produced:
     * <table>
     * <tr><td>Target length</td><td>Result</td></tr>
     * <tr><td>32</td><td>alpha.bravo.charlie.Delta</td></tr>
     * <tr><td>21</td><td>a.bravo.charlie.Delta</td></tr>
     * <tr><td>17</td><td>a.b.charlie.Delta</td></tr>
     * <tr><td>12</td><td>a.b.c.Delta</td></tr>
     * <tr><td>10</td><td>Delta</td></tr>
     * </table>
     *  
     * @param string The String to abbreviate
     * @param targetLength The target length for the abbreviated String
     * @return The abbreviated String.
     */
    public static String abbreviateDotSeparatedString(String string, int targetLength) {
        if (string.length() <= targetLength) {
            return string;
        }

        String[] components = string.split(DOT_SEPARATOR_SPLIT_REGEX);

        int actualLength = string.length();
        StringBuilder builder = new StringBuilder();
        int index = 0;

        while (actualLength > targetLength && index < components.length - 1) {
            String component = components[index++];
            builder.append(component.charAt(0));
            builder.append(DOT_SEPARATOR);

            actualLength -= (component.length() - 1);
        }

        if (actualLength > targetLength) {
            return components[components.length - 1];
        } else {
            for (int i = index; i < components.length; i++) {
                builder.append(components[i]);
                if (i < components.length - 1) {
                    builder.append(DOT_SEPARATOR);
                }
            }
        }

        return builder.toString();
    }

    /**
     * Split a String at the first occurrence of the delimiter.
     * Does not include the delimiter in the result.
     * @param toSplit the string to split
     * @param delimiter to split the string up with
     * @return a two element array with index 0 being before the delimiter, and
     * index 1 being after the delimiter (neither element includes the delimiter);
     * or <code>null</code> if the delimiter wasn't found in the given input String
     */
    public static String[] split(String toSplit, String delimiter) {
        if (!hasLength(toSplit) || !hasLength(delimiter)) {
            return null;
        }
        int offset = toSplit.indexOf(delimiter);
        if (offset < 0) {
            return null;
        }
        String beforeDelimiter = toSplit.substring(0, offset);
        String afterDelimiter = toSplit.substring(offset + delimiter.length());
        return new String[] { beforeDelimiter, afterDelimiter };
    }

    /**
     * Check that the given CharSequence is neither <code>null</code> nor of length 0.
     * Note: Will return <code>true</code> for a CharSequence that purely consists of whitespace.
     * <p><pre>
     * StringUtils.hasLength(null) = false
     * StringUtils.hasLength("") = false
     * StringUtils.hasLength(" ") = true
     * StringUtils.hasLength("Hello") = true
     * </pre>
     * @param str the CharSequence to check (may be <code>null</code>)
     * @return <code>true</code> if the CharSequence is not null and has length
     * @see #hasText(String)
     */
    public static boolean hasLength(CharSequence str) {
        return (str != null && str.length() > 0);
    }

    /**
     * Check that the given String is neither <code>null</code> nor of length 0.
     * Note: Will return <code>true</code> for a String that purely consists of whitespace.
     * @param str the String to check (may be <code>null</code>)
     * @return <code>true</code> if the String is not null and has length
     * @see #hasLength(CharSequence)
     */
    public static boolean hasLength(String str) {
        return hasLength((CharSequence) str);
    }
}

Related

  1. abbreviate(String string, int maxLength)
  2. abbreviate(String text, int maxFront, int maxBack)
  3. abbreviate(String text, Number maxNbrChars)
  4. abbreviate(String time)
  5. abbreviated(final String str, final int maxLength)
  6. abbreviateFileName(final String fileName)
  7. abbreviateInCenter(String stringToAbbreviate, int length)
  8. abbreviateLeft(String s, int width)
  9. abbreviateMessage(String messageBody)