Java String Abbreviate abbreviate(String str, int offset, int maxWidth)

Here you can find the source of abbreviate(String str, int offset, int maxWidth)

Description

abbreviate

License

Open Source License

Declaration

public static String abbreviate(String str, int offset, int maxWidth) 

Method Source Code

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

public class Main {
    public static String abbreviate(String str, int offset, int maxWidth) {
        if (str == null) {
            return null;
        }//  w  w w .  ja  va2 s  .  c  o  m
        if (maxWidth < 4) {
            throw new IllegalArgumentException("Minimum abbreviation width is 4");
        }
        if (str.length() <= maxWidth) {
            return str;
        }
        if (offset > str.length()) {
            offset = str.length();
        }
        if ((str.length() - offset) < (maxWidth - 3)) {
            offset = str.length() - (maxWidth - 3);
        }
        if (offset <= 4) {
            return str.substring(0, maxWidth - 3) + "...";
        }
        if (maxWidth < 7) {
            throw new IllegalArgumentException("Minimum abbreviation width with offset is 7");
        }
        if ((offset + (maxWidth - 3)) < str.length()) {
            return "..." + abbreviate(str.substring(offset), maxWidth - 3);
        }
        return "..." + str.substring(str.length() - (maxWidth - 3));
    }

    public static String abbreviate(String str, int maxWidth) {
        return abbreviate(str, 0, maxWidth);
    }
}

Related

  1. abbreviate(String str, int maxWidth)
  2. abbreviate(String str, int maxWidth)
  3. abbreviate(String str, int maxWidth)
  4. abbreviate(String str, int maxWidth)
  5. abbreviate(String str, int maxWidth)
  6. abbreviate(String str, int preferredLength)
  7. abbreviate(String string, int maxLength)
  8. abbreviate(String text, int maxFront, int maxBack)
  9. abbreviate(String text, Number maxNbrChars)