Here you can find the source of substringBytes(String value, int byte_len)
public static String substringBytes(String value, int byte_len)
//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); } }