Java 按字节大小分割字符串,防止将中文切掉

import java.io.UnsupportedEncodingException;

public class StringUtil {

	private static int spiltSize = 256;
	private static String spiltString(String resource, int index) {
		if (resource == null)
			return "String is null";
		byte[] strByte = resource.getBytes();
		int byteLength = strByte.length;
		if (index <= 0 || index > byteLength)
			return "out of index for this string";
		int strLength = resource.length();
		String[] tmpByte = new String[strLength];
		for (int i = 0; i < strLength; i++) {
			tmpByte[i] = resource.substring(i, i + 1);
		}
		int myIndex = getIndex(tmpByte, strLength, index);
		byte[] resultByte = getBytesByIndex(tmpByte, myIndex);
		if (resultByte == null)
			return "error";
		String result = new String(resultByte);
		return result;
	}

	private static int getIndex(String[] tmpByte, int length, int index) {
		int byteTotleMin = 0;
		int myIndex = -1;
		for (int i = 0; i < length; i++) {
			byteTotleMin = byteTotleMin + tmpByte[i].getBytes().length;
			if (byteTotleMin >= index) {
				myIndex = i;
				break;
			}
		}
		return myIndex;
	}

	private static byte[] getBytesByIndex(String[] tmpByte, int index) {
		if (index > tmpByte.length)
			return null;
		index++;
		byte[][] tmp = new byte[index][];
		int count = 0;
		for (int i = 0; i < index; i++) {
			tmp[i] = tmpByte[i].getBytes();
			count = count + tmp[i].length;
		}
		byte[] result = new byte[count];
		int one = tmp.length;
		int indexTmp = 0;
		for (int i = 0; i < one; i++) {
			int two = tmp[i].length;
			for (int j = 0; j < two; j++) {
				result[indexTmp] = tmp[i][j];
				indexTmp++;
			}
		}
		return result;
	}

	private static String[] getSpiltMessage(String messageContent)
			throws UnsupportedEncodingException {
		String message = messageContent;
		int byteLen = message.getBytes("UTF-8").length;
		int len = byteLen / spiltSize + 1;
		String[] strArr = new String[len];
		if (byteLen > spiltSize) {
			for (int i = 0; i < len; i++) {
				StringBuffer strTemp = new StringBuffer();
				for (int j = 0; j < i; j++) {
					if (!"null".equals(strArr[j].toString())) {
						strTemp.append(strArr[j]);
					}
				}
				int start = message.indexOf(strTemp.toString());
				if (start == -1) {
					start = 0;
				}
				// 对null情况做特殊处理
				int start2 = strTemp.toString().length();
				if (start2 == 4) {
					start2 = 0;
				}
				String subTemp = "";
				if (start + start2 == 0) {
					subTemp = message;
				} else {
					// 找到切割点
					subTemp = message.substring(start + start2);
				}
				if (subTemp.getBytes().length > spiltSize) {
					strArr[i] = spiltString(subTemp, spiltSize);
					// 再次检测一下字节大小,保证要小于256字节
					if (strArr[i].getBytes("UTF-8").length > spiltSize) {
						strArr[i] = strArr[i].substring(0,
								strArr[i].length() - 9);
						}
				}else{
					strArr[i] = subTemp;
				}
			}

		} else {
			strArr[0] = message;
		}
		// 补全256位
		for (int k = 0; k < len; k++) {
			if (null != strArr[k]
					&& strArr[k].getBytes("UTF-8").length < spiltSize) {
				StringBuffer strnull = new StringBuffer();
				for (int y = 0; y < spiltSize
						- strArr[k].getBytes("UTF-8").length; y++) {
					strnull.append(" ");
				}
				strArr[k] = strArr[k] + strnull.toString();
				// System.out.print(strArr[k].getBytes("UTF-8").length);
			}
			// System.out.println(strArr[k]);
			}
		return strArr;
	}

	public static void main(String[] args) throws UnsupportedEncodingException {
		// String message =
		// "311111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111122";
		// String message =
		// "311111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111443111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111114455";
		String message = "11邮送欺诈邮送欺诈邮送欺诈邮件近期发现一些不法分子假冒公司管理员的名义向我司不少员工发送欺诈邮件送欺诈邮件近期发现一些不法分子假冒公司管理员的名义向我司不少员工444邮件件333件33件11邮送欺诈邮送欺诈邮送欺诈邮件近期发现一些不法分子假冒公司管理员的名义向我司不少员工发送欺诈邮件送欺诈邮件近期发现一些不法分子假冒公司管理员的名义向我司不少员工444邮件件333件33件";
		String[] strArr = getSpiltMessage(message);
		for (int k = 0; k < strArr.length; k++) {
			System.out.print(strArr[k].getBytes("UTF-8").length);
			System.out.println(strArr[k]);
		}
	}

}

 

你可能感兴趣的:(java)