Java String Shorten shorten(String s, int length, String suffix)

Here you can find the source of shorten(String s, int length, String suffix)

Description

shorten

License

Open Source License

Declaration

public static String shorten(String s, int length, String suffix) 

Method Source Code

//package com.java2s;
/**//from w ww.j ava 2s . c om
 * Copyright (c) 2000-2003 Liferay Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

public class Main {
    public static String shorten(String s) {
        return shorten(s, 20);
    }

    public static String shorten(String s, int length) {
        return shorten(s, length, "..");
    }

    public static String shorten(String s, String suffix) {
        return shorten(s, 20, suffix);
    }

    public static String shorten(String s, int length, String suffix) {
        if (s == null || suffix == null) {
            return null;
        }

        if (s.length() > length) {
            s = s.substring(0, length) + suffix;
        }

        return s;
    }

    public static String substring(String src, int begin, int end) {
        if (src == null || "".equals(src))
            return "";
        int len = src.length();
        if (end > len)
            end = len;
        return src.substring(begin, end);
    }
}

Related

  1. shorten(String s)
  2. shorten(String s)
  3. shorten(String s, int len)
  4. shorten(String s, int length)
  5. shorten(String s, int length, String suffix)
  6. shorten(String s, int max)
  7. shorten(String s, int maxLength)
  8. shorten(String script, int length)
  9. shorten(String str, int length)