Skip to content

Instantly share code, notes, and snippets.

@arahansa
Created March 2, 2016 01:52
Show Gist options
  • Save arahansa/e29eb5fcabc6d503a0e3 to your computer and use it in GitHub Desktop.
Save arahansa/e29eb5fcabc6d503a0e3 to your computer and use it in GitHub Desktop.
한글 3바이트 처리하는 자바처리
@Test
public void koreanCheck2() throws Exception {
String msg ="일이>삼사오육칠팔구십";
byte[] test = msg.getBytes("UTF-8");
System.out.println("총길이 :"+test.length);
int lastPosition = 0;
for(int i=0;i<20;i++){
int iChar = (int) test[i];
if ((iChar > 127) || (iChar < 0)) {
// 한글의 경우(2byte 통과처리)
// 한글은 2Byte이기 때문에 다음 글자는 볼것도 없이 스킵한다
i+=2; // 한개 더 해서 3byte
}
if(i<20)
lastPosition=i;
System.out.println("현재 라스트 포지션 :"+ lastPosition);
}
lastPosition++;
System.out.println("라스트 포지션 :"+lastPosition);
System.out.println("서브스트링 :"+new String(test, 0, lastPosition));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment