Java String Shorten shorten(String nameForUI, int maxLen)

Here you can find the source of shorten(String nameForUI, int maxLen)

Description

shorten

License

Open Source License

Declaration

public static String shorten(String nameForUI, int maxLen) 

Method Source Code

//package com.java2s;
/******************************************************************************
 * Copyright (C) 2012-2013  Fabio Zadrozny and others
 *
 * 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:/* www. jav  a 2s. co m*/
 *     Fabio Zadrozny <fabiofz@gmail.com>    - initial API and implementation
 *     Jonah Graham <jonah@kichwacoders.com> - ongoing maintenance
 ******************************************************************************/

public class Main {
    public static String shorten(String nameForUI, int maxLen) {
        if (nameForUI.length() >= maxLen) {
            maxLen -= 5;
            int first = maxLen / 2;
            int last = maxLen / 2 + (maxLen % 2);

            return nameForUI.substring(0, first)
                    + " ... "
                    + nameForUI.substring(nameForUI.length() - last,
                            nameForUI.length());
        }
        return nameForUI;
    }
}

Related

  1. shorten(String input, int length, boolean wholeWord)
  2. shorten(String label, int maxLength)
  3. shorten(String line)
  4. shorten(String msg, int front, String join, int end)
  5. shorten(String name, int max)
  6. shorten(String pkg, boolean shorten)
  7. shorten(String s)
  8. shorten(String s)
  9. shorten(String s)