Java String Sub String subStr(String str, int limit)

Here you can find the source of subStr(String str, int limit)

Description

sub Str

License

Apache License

Declaration

public static String subStr(String str, int limit) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static String subStr(String str, int limit) {
        String result = str.substring(0, 17);
        int subLen = 17;
        for (int i = 0; i < limit; i++) {
            if (limit < getEncodingByteLen(
                    str.substring(0, (subLen + i) > str.length() ? str.length() : (subLen)))) {
                result = str.substring(0, subLen + i - 1);
                break;
            }/*from ww w  .  ja  v a  2  s . c  om*/
            if ((subLen + i) > str.length()) {
                result = str.substring(0, str.length() - 1);
                break;
            }
        }
        return result;
    }

    public static int getEncodingByteLen(String sub) {
        int zhLen = (sub.getBytes().length - sub.length()) * 2;
        int enLen = sub.length() * 2 - sub.getBytes().length;
        return zhLen + enLen;
    }
}

Related

  1. substr(String str, int beginIndex, int endIndex)
  2. substr(String str, int iLen)
  3. substr(String str, int index)
  4. subStr(String str, int len)
  5. substr(String str, int length)
  6. subStr(String str, int maxLen)
  7. substr(String str, int startIndex, int length)
  8. substr(String substr1, String substr2)
  9. substr(String what, int begin, int end)