Java String abbreviate in middle

Description

Java String abbreviate in middle



//package com.demo2s;
import java.io.UnsupportedEncodingException;

public class Main {
    public static void main(String[] argv) throws Exception {
        String str = "www.demo2s.com this is a test";
        String middle = "...";
        int length = 20;
        System.out.println(abbreviateMiddle(str, middle, length));
    }// w ww .  j a  v  a  2 s . c  om

    public static String abbreviateMiddle(String str, String middle, int length) {
        if (!isEmpty(str) && !isEmpty(middle)) {
            if (length < str.length() && length >= middle.length() + 2) {
                int targetSting = length - middle.length();
                int startOffset = targetSting / 2 + targetSting % 2;
                int endOffset = str.length() - targetSting / 2;
                StringBuilder builder = new StringBuilder(length);
                builder.append(str.substring(0, startOffset));
                builder.append(middle);
                builder.append(str.substring(endOffset));
                return builder.toString();
            } else {
                return str;
            }
        } else {
            return str;
        }
    }

    public static boolean isEmpty(CharSequence cs) {
        return cs == null || cs.length() == 0;
    }
}

//package com.demo2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String str = "www.demo2s.com";
        String middle = "...";
        int length = 10;
        System.out.println(abbreviateMiddle(str, middle, length));
    }/*from   w w  w .  j  a v  a 2  s  .  co m*/

    public static String abbreviateMiddle(final String str, final String middle, final int length) {
        if (isBlank(str) || isBlank(middle)) {
            return str;
        }

        if (length >= str.length() || length < middle.length() + 2) {
            return str;
        }

        final int targetSting = length - middle.length();
        final int startOffset = targetSting / 2 + targetSting % 2;
        final int endOffset = str.length() - targetSting / 2;

        final StringBuilder builder = new StringBuilder(length);
        builder.append(str.substring(0, startOffset));
        builder.append(middle);
        builder.append(str.substring(endOffset));

        return builder.toString();
    }

    public static boolean isBlank(String str) {
        if (str == null || "".equals(str)) {
            return true;
        }
        return false;
    }
}
//package com.demo2s;

public class Main {
   public static void main(String[] argv) throws Exception {
      String stringToAbbreviate = "www.demo2s.com";
      int length = 10;
      System.out.println(abbreviateInCenter(stringToAbbreviate, length));
   }//from  ww w  .j  ava  2 s.co m

   private static final String ELLIPSIS = "...";

   public static String abbreviateInCenter(String stringToAbbreviate, int length) {
      if (stringToAbbreviate == null) {
         return "";
      }
      int halfLength = length / 2;
      int firstPartLastIndex = halfLength - ELLIPSIS.length();
      int stringLength = stringToAbbreviate.length();
      return String.format("%s%s%s", stringToAbbreviate.substring(0, firstPartLastIndex), ELLIPSIS,
            stringToAbbreviate.substring(stringLength - halfLength, stringLength));
   }
}



PreviousNext

Related