Java String Truncate truncate(String strOrginal, int iByteMaxSize)

Here you can find the source of truncate(String strOrginal, int iByteMaxSize)

Description

truncate

License

Open Source License

Declaration

public static final String truncate(String strOrginal, int iByteMaxSize) 

Method Source Code

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

public class Main {

    public static final String truncate(String strOrginal, int iByteMaxSize) {
        int iByteLen = 0;
        final int strLen = strOrginal.length();
        if (iByteMaxSize < 4) {
            return "...".substring(0, iByteMaxSize);
        }//from www . ja  v  a  2 s .  co m
        for (int i = 0; i < strLen; i++) {
            iByteLen += ((strOrginal.charAt(i) & 0xFF00) == 0) ? 1 : 2;
            if (iByteLen > iByteMaxSize) {
                for (; iByteLen > (iByteMaxSize - 3); i--) {
                    iByteLen -= ((strOrginal.charAt(i) & 0xFF00) == 0) ? 1 : 2;
                } // for
                return strOrginal.substring(0, i + 1) + "...";
            } // if
        } // for
        return strOrginal;
    }
}

Related

  1. truncate(String string, final int length)
  2. truncate(String string, int length)
  3. truncate(String string, int maxLength, boolean atBeginning, String ellipse)
  4. truncate(String string, int maxSize)
  5. truncate(String string, int n, String suffix)
  6. truncate(String text)
  7. truncate(String text, int len)
  8. truncate(String text, int length)
  9. truncate(String text, int length)