Java String Sub String subStrBytes2(String str, int byteLength)

Here you can find the source of subStrBytes2(String str, int byteLength)

Description

sub Str Bytes

License

Open Source License

Declaration

public static String[] subStrBytes2(String str, int byteLength) 

Method Source Code

//package com.java2s;

public class Main {

    public static String[] subStrBytes2(String str, int byteLength) {
        String[] result = new String[] { "", "" };

        if (str != null && getStringByteLength(str) > byteLength) {
            String tmp = "";
            char c;
            int iCount = 0;
            int i = 0;
            for (i = 0; iCount < byteLength; i++) {
                c = str.charAt(i);/*from  w w w. j  a va2 s  .  c om*/
                if (c < 0x7f) {
                    iCount++;
                } else {
                    iCount += 2;
                }
                tmp = tmp + c;
            }
            for (int j = i; j < str.length(); j++) {
                result[1] += str.charAt(j);
            }

            result[0] = tmp;
        } else {
            result[0] = str;
        }

        return result;
    }

    public static int getStringByteLength(String str) {
        char c;
        int iCount = 0;
        for (int i = 0; str != null && i < str.length(); i++) {
            c = str.charAt(i);
            if (c < 0x7f) {
                iCount++;
            } else {
                iCount += 2;
            }
        }
        return iCount;
    }
}

Related

  1. substraction2(double[] a, double[] b)
  2. substractPrefixPostfix(Object obj, String prefix, String suffix)
  3. subStrBeforeDotNotIncludeDot(String str)
  4. subStrByBytes(String str, int len, String tail)
  5. substrByLength(String str, int start, int size, String markCode)
  6. substrCount(String haystack, String needle)
  7. subStrIfNeed(final String str, int num)
  8. substring(byte[] array, int start)
  9. substring(byte[] src, int start, int len)