JAVA & Open Framework
[펌] 자바 한글 바이트단위로 자르기
아로나
2012. 8. 6. 14:40
<%@ page contentType="text/html;charset=euc-kr" %>
<%
String title = "大韓民國에는 1km마다 맛있는 맛집들이 많이 있습니다!!!";
title = cutStr(title,17);
out.println(title);
%>
<%!
public static String cutStr(String str,int cutByte)
{
byte [] strByte = str.getBytes();
if( strByte.length < cutByte )
return str;
int cnt = 0;
for( int i = 0; i < cutByte; i++ )
{
if( strByte[i] < 0 )
cnt++;
}
String r_str;
if(cnt%2==0) r_str = new String(strByte, 0, cutByte );
else r_str = new String(strByte, 0, cutByte + 1 );
return r_str;
}
%>
[출처] JSP 한글,한자가 섞인 문자열 바이트 수대로 자르기|작성자 소화