Java String Clip clipString(String input, int numChars, boolean appendEllipses)

Here you can find the source of clipString(String input, int numChars, boolean appendEllipses)

Description

Reduces the input string to the number of chars, or its length if the number of chars exceeds the input string's length

License

Open Source License

Parameter

Parameter Description
input The string to clip
numChars the number of leading chars to keep (all others will be removed)

Return

: the clipped string

Declaration

public static String clipString(String input, int numChars, boolean appendEllipses) 

Method Source Code

//package com.java2s;
/*//from   w  w w . ja v  a 2s  .  com
 * Copyright 2010-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file 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 {
    /**
     * Reduces the input string to the number of chars, or its length if the
     * number of chars exceeds the input string's length
     *
     * @param input The string to clip
     * @param numChars the number of leading chars to keep (all others will be
     *            removed)
     * @return: the clipped string
     */
    public static String clipString(String input, int numChars, boolean appendEllipses) {
        int end = Math.min(numChars, input.length());
        String output = input.substring(0, end);
        if (appendEllipses) {
            output = (output.length() < input.length()) ? output + "..." : output;
        }
        return output;
    }
}

Related

  1. clip(final String s, final int leftClip, final int rightClip)
  2. clip(String characterItem)
  3. clip(String str, int startChars)
  4. ClipObjectReferenceAddress(String p_fullObjectReference)
  5. clipText(String text, int maxLength)