Java String Sub String substringBytes(String value, int byte_len)

Here you can find the source of substringBytes(String value, int byte_len)

Description

substring Bytes

License

Open Source License

Declaration

public static String substringBytes(String value, int byte_len) 

Method Source Code

//package com.java2s;
/*/*from  ww w .  j  a  v a 2  s .  c om*/
 *    GeoTools - The Open Source Java GIS Toolkit
 *    http://geotools.org
 *
 *    (C) 2014, Open Source Geospatial Foundation (OSGeo)
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 */

public class Main {
    public static String substringBytes(String value, int byte_len) {
        int retLength = 0;
        int tempSize = 0;
        for (int index = 1; index <= value.length(); index++) {
            int asc = value.charAt(index - 1);
            if (asc > 127) {
                if (byte_len >= tempSize + 2) {
                    tempSize += 2;
                    retLength++;
                } else {
                    return value.substring(0, retLength);
                }
            } else {
                if (byte_len > tempSize) {
                    tempSize++;
                    retLength++;
                }
            }
        }
        return value.substring(0, retLength);
    }
}

Related

  1. substringByByteCount(String str, int byteCount)
  2. subStringByBytes(String str, int toCount, String more)
  3. substringByCodePoint(String inputStr, int codePointStart, int codePointEnd)
  4. substringByMask(String baseString, String baseMask, String subMask)
  5. subStringByte(String str, int toCount, String more)
  6. substringCompare(String string, int index, char... characters)
  7. substringDelimited(String string, String start, String end, int startingPosition)
  8. substringEL(String str, int index, String defaultValue)
  9. substringEnd(String string, int start, int length)