JavaMaster.org
자바 공유
Search
Category
전체
(9)
Java
(6)
Tomcat
(1)
JavaScript
(1)
Ajax
(0)
My-SQL
(1)
Calendar
«
2010/09
»
일
월
화
수
목
금
토
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Tags
자바
JDBC
자바유틸
자바스크립트
구문강조
SyntaxHighlighter
DBCP
mysql5
mysql설치
WTP
Eclipse
웹디비
디비유틸
톰캣
톰캣설치
tomcat
New Articles
[자바 JDBC:강좌] JDBC..
(5)
[자바 JDBC:강좌] JDBC..
[자바:유틸] 웹디비( 디..
(5)
[자바 JDBC:강좌] JDBC..
[자바:강좌] WTP(Eclise..
[Mysql:강좌] Mysql5 설..
(1)
[톰캣:강좌] 초보자를 위..
(1)
New Replies
스카이뮤직 뮤직스카..
스카이 - 03/26
스카이뮤직 뮤서치 개..
스카이 - 03/24
바로가기=>http://lic..
좋은자료 - 03/07
바로가기=>http://lic..
좋은자료 - 03/06
발*기부전 // 데카원..
데카원 - 02/19
스칼라가 왔구나 왔어..
스칼라 체지방 초전박살 - 02/12
스칼라가 왔구나 왔어..
스칼라 체지방 초전박살 - 02/12
New Trackback
Links
Archive
2007/10
(2)
2007/06
(2)
2007/04
(5)
Counter
Total : 28614
Today : 9
Yesterday : 12
Skin is Grayfox v0.9b
Design by Aki_Rose
[자바:TIP] 자바 문자열 함수 몇가지
Java - 2007/04/04 11:42
첫번째 올리는 글입니다.^^
시간나는 데로 팁을 올려 보도록 하겠습니다.
/* 문자열 치환 */ public static String replaceStr(String src, String oldstr, String newstr){ if (src == null) return null; StringBuffer dest = new StringBuffer(""); int len = oldstr.length(); int srclen = src.length(); int pos = 0; int oldpos = 0; while ((pos = src.indexOf(oldstr, oldpos)) >= 0) { dest.append(src.substring(oldpos, pos)); dest.append(newstr); oldpos = pos + len; } if (oldpos < srclen) dest.append(src.substring(oldpos, srclen)); return dest.toString(); } /** * int형 숫자앞에 원하는 size의 자릿수에 맞게 '0'를 붙인다. 예) 000000000 (일억자리수)에 345를 넣었을 경우 * '000000345'반환 * * @param str * 대상 int형 숫자 * @param size * 원하는 자릿수 * @return 대상문자열에 자릿수 만큼의 '0'를 붙인 문자열 */ public static String zerofill(int num, int size) throws Exception { return zerofill(new Integer(num), size); } public static String zerofill(double num, int size) throws Exception { return zerofill(new Double(num), size); } public static String zerofill(String num, int size) throws Exception { StringBuffer sb = new StringBuffer(); for (int i = 0; i < size - num.getBytes().length; i++) { sb.append("0"); } sb.append(num); return sb.toString(); } /** * Number형 숫자앞에 원하는 size의 자릿수에 맞게 '0'를 붙인다. 예) 000000000 (일억자리수)에 345를 넣었을 * 경우 '000000345'반환 * * @param num * 대상 Number형 숫자 * @param size * 원하는 자릿수 * @return 대상문자열에 자릿수 만큼의 '0'를 붙인 문자열 */ public static String zerofill(Number num, int size) throws Exception { String zero = ""; for (int i = 0; i < size; i++) { zero += "0"; } DecimalFormat df = new DecimalFormat(zero); return df.format(num); } /** * 게시판 제목 같은 글을 원하는 길이로 자릅니다. 한글( or 한글 + 영어) 제목을 자르면 깨지는걸 막기위한 메소드이다. 반환될 String 의 길이는 len과 tail의 length를 더한 길이가 될 것이다. 마지막 character가 공백이면 이 공백은 * 제거된다. * * @param str : * 원래의 글 * @param len : * 자르고자 하는 길이 * @param tail : * len 만큼 자르고 뒤에 붙일 꼬리글 */ public static String getCutString(String str, int len, String tail) { byte[] by = str.getBytes(); int count = 0; try { for(int i = 0; i < len; i++) { if((by[i] & 0x80) == 0x80) count++; } if((by[len - 1] & 0x80) == 0x80 && (count % 2) == 1) len--; return new String(by, 0, len)+tail; } catch(java.lang.ArrayIndexOutOfBoundsException e) { return str; } } public static String getCutUTFString(String str, int len, String tail) { if (str.length() <= len) { return str; } StringCharacterIterator sci = new StringCharacterIterator(str); StringBuffer buffer = new StringBuffer(); buffer.append(sci.first()); for (int i = 1; i < len; i++) { if (i < len - 1) { buffer.append(sci.next()); } else { char c = sci.next(); if (c != 32) { // 마지막 charater가 공백이 아닐경우 buffer.append(c); } } } buffer.append(tail); return buffer.toString(); }
자바
,
자바유틸
트랙백
(0)
댓글
(0)
이 글의 트랙백 주소 :
http://javamaster.org/tt/trackback/1
네티켓을 지켜주세요! 따뜻한 人터넷을 만들어가요.
Name.
Password.
Secret.
Home.
◀ PREV
[
1
]
...
[
5
]
[
6
]
[
7
]
[
8
]
[
9
]
NEXT ▶
shin-gosoo
’s Blog with
Tattertools 1.1.2.2 : Animato
/
Grayfox 0.9b