Android String Sub String Get subStringByByte(String str, int startPos, int length)

Here you can find the source of subStringByByte(String str, int startPos, int length)

Description

sub String By Byte

Declaration

public static String subStringByByte(String str, int startPos,
            int length) 

Method Source Code

//package com.java2s;

public class Main {
    public static String subStringByByte(String str, int startPos,
            int length) {
        if (str == null || str.length() == 0) {
            return "";
        }/* w  w  w .j a  v  a 2  s.c o  m*/

        StringBuffer sb = new StringBuffer();
        int byteLen = 0;
        for (int i = 0; i < str.length(); i++) {
            int ascii = Character.codePointAt(str, i);
            if (ascii >= 0 && ascii <= 255) {
                byteLen++;
            } else {
                byteLen += 2;
            }
            if (byteLen >= startPos + 1 && byteLen <= length) {
                sb.append(str.charAt(i));
            } else {
                break;
            }
        }
        return sb.toString();
    }
}

Related

  1. delSuffix(String str)
  2. deleteLast(String str, int num)
  3. deleteLastIf(String str, String eq_str)
  4. getTailorString(String string, int length)
  5. mid(String str, int beginIndex, int length)
  6. subStringByByte(String paramString, int beginIndex, int endIndex)
  7. subString(String source, String start, int offsetStart, String end, int offsetEnd)
  8. subString(String str, int length)
  9. subInStringByFlag(String str, String flag)