Java 한글 Encoding
한글 포함한 문자열을 바이트 단위로 잘라야 하는 경우 아래 코드사용
new String("문자열".getBytes("EUC-KR"), start, end, "EUC-KR")
Source
public class TestMain { public static void main(String[] args) throws Exception { System.out.println("START"); System.out.println("-----------------------------------------"); test("한글입니다"); test("AB영어입니"); System.out.println("-----------------------------------------"); System.out.println("END"); } private static void test(String line) throws Exception { String tab = " "; System.out.println(); System.out.println(line); System.out.println(tab + "line.getBytes().length : " + line.getBytes().length); System.out.println(tab + "line.getBytes(\"EUC-KR\").length : " + line.getBytes("EUC-KR").length); System.out.println(tab + "new String(line.getBytes(), 0, 2) : " + (new String(line.getBytes(), 0, 2))); System.out.println(tab + "new String(line.getBytes(\"EUC-KR\"), 0, 2, \"EUC-KR\") : " + (new String(line.getBytes("EUC-KR"), 0, 2, "EUC-KR"))); } } |
Console
START ----------------------------------------- 한글입니다 line.getBytes().length : 15 line.getBytes("EUC-KR").length : 10 new String(line.getBytes(), 0, 2) : � new String(line.getBytes("EUC-KR"), 0, 2, "EUC-KR") : 한 AB영어입니 line.getBytes().length : 14 line.getBytes("EUC-KR").length : 10 new String(line.getBytes(), 0, 2) : AB new String(line.getBytes("EUC-KR"), 0, 2, "EUC-KR") : AB ----------------------------------------- END |