Java按照字节截取字符串
public static List<String> chineseSplitFunction(String src, String byteType, int bytes) {
if (StringUtils.isBlank(src))
{
return Collections.EMPTY_LIST;
}
List<String> splitList = new ArrayList<String>();
int startIndex = 0;
int endIndex = bytes > src.length() ? src.length() : bytes;
try
{
while (startIndex < src.length())
{
String subString = src.substring(startIndex, endIndex);
while (subString.getBytes(byteType).length > bytes)
{
--endIndex;
subString = src.substring(startIndex, endIndex);
}
splitList.add(src.substring(startIndex, endIndex));
startIndex = endIndex;
endIndex = (startIndex + bytes) > src.length() ? src.length() : startIndex + bytes;
}
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
LogUtil.error(e);
}
return splitList;
}
public static void main(String[] args) {
String data = "超长字符串报文且包含中文";
List<String> splitDataList = chineseSplitFunction(data, "UTF-8", 3600);
}