Java String Truncate truncate(String string, final int length)

Here you can find the source of truncate(String string, final int length)

Description

truncate

License

Open Source License

Declaration

public static String truncate(String string, final int length) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static String truncate(String string, final int length) {
        if (string.length() > length) {
            string = string.substring(0, length);
        }//from w ww  .ja va 2  s  . c o m
        return string;
    }

    public static String substring(final String string, int fromIndex, int toIndex) {
        final int len = string.length();
        if (fromIndex < 0) {
            fromIndex += len;
            if (toIndex == 0) {
                toIndex = len;
            }
        }
        if (toIndex < 0) {
            toIndex += len;
        }
        if (fromIndex < 0) {
            fromIndex = 0;
        }
        if (toIndex > len) {
            toIndex = len;
        }
        if (fromIndex >= toIndex) {
            return "";
        }
        return string.substring(fromIndex, toIndex);
    }
}

Related

  1. truncate(String str, int maxSize)
  2. truncate(String str, int n)
  3. truncate(String str, int size, boolean useEllipsis)
  4. truncate(String str, int start, int end)
  5. truncate(String str, String prefix, String suffix)
  6. truncate(String string, int length)
  7. truncate(String string, int maxLength, boolean atBeginning, String ellipse)
  8. truncate(String string, int maxSize)
  9. truncate(String string, int n, String suffix)