개발관련/JAVA

[JAVA] 문자열 byte 확인, byte 단위로 자르기

90만식 2022. 8. 3. 10:13
728x90
public class testPjt {

	public static void main(String[] args) {
		String str = "가나다라마바사아자차  1111";
		int cutBytes = 30;

		String result = new String(str.getBytes(), 0, cutBytes);
		System.out.println(result);

	}
}

문자열 길이를 byte 단위로 가져오는 코드.

UTF-8 기준 한글 3Byte, 알파벳, 숫자, 띄어쓰기 1Byte

public class testPjt {

	public static void main(String[] args) {


		String errorMsg = "가나다라마바사아자차";

		int byteLen = errorMsg.getBytes().length;

		System.out.println(byteLen);
	}
}

Byte 로 문자열을 자르는 방법

 

728x90