Java String Truncate truncate(String s, int stop, String suf)

Here you can find the source of truncate(String s, int stop, String suf)

Description

truncate

License

Apache License

Parameter

Parameter Description
s a parameter
stop a parameter
suf a parameter

Return

String truncated

Declaration

public static final String truncate(String s, int stop, String suf) 

Method Source Code

//package com.java2s;
/**//from   w w w.  j a  va2 s. c  o m
 *  Copyright 2011 Marco Berri - marcoberri@gmail.com
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
 **/

public class Main {
    /**
     *
     * @param s
     * @param stop
     * @param suf
     * @return String truncated
     */
    public static final String truncate(String s, int stop, String suf) {
        if (s == null) {
            return "";
        }
        if (s.length() == 0) {
            return "";
        } else if (s.length() <= stop) {
            return s;
        } else {
            return substring(s, 0, s.indexOf(" ", stop)) + suf;
        }
    }

    /**
     *
     * @param s
     * @param start
     * @param stop
     * @return string
     */
    public static final String substring(String s, int start, int stop) {
        if (isEmpty(s)) {
            return s;
        }
        if (start < 0 || start >= s.length()) {
            return "";
        }
        if (stop > s.length() || stop < 0) {
            stop = s.length();
        }
        return s.substring(start, stop);
    }

    /**
     *
     * @param s
     * @param start
     * @return string
     */
    public static final String substring(String s, int start) {
        return substring(s, start, -1);
    }

    /**
     * Checks if a String is empty or null
     *
     *   @param s String to check
     * @return true if null or empty, false otherwise
     */
    public static boolean isEmpty(String s) {
        return (s == null || "".equals(s.trim()));
    }

    /**
     *
     * @param s
     * @param alt
     * @return
     */
    public static String isEmpty(String s, String alt) {
        if (isEmpty(s)) {
            return alt;
        }
        return s;
    }

    /**
     * Esegue una Trim su una Stringa
     * se s == null return null
     *
     *   @param s String to trim
     * @return La Stringa Trimmata
     */
    public static final String trim(String s) {
        if (s == null) {
            return null;
        }
        return s.trim();
    }
}

Related

  1. truncate(String s, int length, boolean indicator)
  2. truncate(String s, int max_len)
  3. truncate(String s, int maxLength)
  4. truncate(String s, int maxLength)
  5. truncate(String s, int maxLength, boolean alignRight)
  6. truncate(String s, int truncLength, boolean removeWhiteSpace)
  7. truncate(String source, int maxLength, String cutoffReplacement)
  8. truncate(String src, int maxChars, boolean addEllipses)
  9. truncate(String str)