예전에 네이버 블로그에서 작성한 것인데... 필요해 보이는 것만 하나둘 옮겨봅니다.
public class CutStringTest {
/**
* 지정한 정수의 개수 만큼 빈칸(" ")을 스트링을 구한다.
*
* @param int 문자 개수
* @return String 지정된 개수 만큼의 빈칸들로 연결된 String
*/
public static String spaces(int count) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < count; i++) {
sb.append(' ');
}
return sb.toString();
}
/**
* 지정한 정수의 개수 만큼 빈칸(" ")을 스트링을 구한다. 절단된 String의 바이트 수가 자를 바이트 개수보다 모자라지 않도록
* 한다.
*
* @param str
* 원본 String
* @param int 자를 바이트 개수
* @return String 절단된 String
*/
public static String cutStringByBytes(String str, int length) {
byte[] bytes = str.getBytes();
int len = bytes.length;
int counter = 0;
if (length >= len) {
return str + spaces(length - len);
}
for (int i = length - 1; i >= 0; i--) {
if (((int) bytes[i] & 0x80) != 0)
counter++;
}
return new String(bytes, 0, length + (counter % 2));
}
/**
* 지정한 정수의 개수 만큼 빈칸(" ")을 스트링을 구한다. 절단된 String의 바이트 수가 자를 바이트 개수를 넘지 않도록 한다.
*
* @param str
* 원본 String
* @param int 자를 바이트 개수
* @return String 절단된 String
*/
public static String cutInStringByBytes(String str, int length) {
byte[] bytes = str.getBytes();
int len = bytes.length;
int counter = 0;
if (length >= len) {
return str + spaces(length - len);
}
for (int i = length - 1; i >= 0; i--) {
if (((int) bytes[i] & 0x80) != 0)
counter++;
}
return new String(bytes, 0, length - (counter % 2));
}
/**
* Main
* @param args
*/
public static void main(String[] args) {
String str = "글자 자르기";
for (int i = 4; i < 24; i++)
System.out.println(i + ": [" + cutInStringByBytes(str, i) + "]");
}
}
'Java' 카테고리의 다른 글
Parse an XML File using the SAX Parser (0) | 2015.07.15 |
---|---|
Maria DB Connection By Java (0) | 2015.07.06 |