티스토리 뷰

쓸때마다 새로만드는 것 같아서 저장용


UTF-8일 경우

subStringBytes("블라블라블라라", 10, 3);

EUC-KR일 경우

subStringBytes("블라블라블라라", 10, 2);


public String subStringBytes(String str, int byteLength, int sizePerLetter) {
	int retLength = 0;
	int tempSize = 0;
	int asc;
	if (str == null || "".equals(str) || "null".equals(str)) {
		str = "";
	}

	int length = str.length();

	for (int i = 1; i <= length; i++) {
		asc = (int) str.charAt(i - 1);
		if (asc > 127) {
			if (byteLength >= tempSize + sizePerLetter) {
				tempSize += sizePerLetter;
				retLength++;
			}
		} else {
			if (byteLength > tempSize) {
				tempSize++;
				retLength++;
			}
		}
	}

	return str.substring(0, retLength);
}


댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함