Class String
java.lang.Object
java.lang.String
※ 문자열 연산이 필요한 경우 StringBuffer나 StringBuilder를 사용한다.
Link - String 클래스를 함부로 사용하면 안되는 이유
● String[] split(String regex[, int limit]) : regex 를 기준, 문자열을 분할하여 배열에 할당
1 2 3 | String txt = "가, 나, 다, 라, 마, 바" ;
String strs[] = text.split( ", " );
int arrayLength = "1/2/3/4/5" .split( "," ).length();
|
● int length() : 문자열의 길이를 리턴. Returns the length of this string.
1 | boolean result = (@.length() >= 4 && @.length() <= 12 ) ? true : false ;
|
● char charAt(int index) : 지정한 인덱스의 문자를 리턴. Returns the char value at the specified index.
1 2 3 4 | String s3 = "안녕하세요" ;
char c3 = s3.charAt( 3 );
System.out.println( "s3의 3번째 문자 : " +c3);
|
● int indexOf(int ch[, int fromIndex]) : 문자열에서 인자값을 찾은뒤 안자값의 위치를 반환
1 2 3 4 5 6 | String s4 = "안녕하세요~ 홍길동님~" ;
int n4 = s4.indexOf( "홍" );
if (uri.indexOf( "created_ok.kh" ) != - 1 ){}
|
● int lastIndexOf(int ch[, int fromIndex]) : 역방향검색
● int lastIndexOf(String word, int startIndex) : 검색할 문자열과 시작값 지정
indexOf 좌 -> 우로 검색
lastIndexOf 우 -> 좌로 검색
※ 주의
역방향으로 검색한다고 해서 index값이 바뀌진 않는다.
즉, 우측에서 두번째에 있는 값을 찾았다고 해도 리턴값이 2가 되는것은 아니라는 말
● String toLowerCase() : 문자열을 모두 소문자로 반환
● String toUpperCase() : 문자열을 모두 대문자로 반환
1 2 | println((wholeText.toLowerCase().indexOf(search.toLowerCase()) >= 0 ) ? "발견" : "없음" );
|
● boolean startsWith(String word) : 매개변수의 문자열로 시작하는지
● boolean endsWith(String word) : 매개변수의 문자열로 끝나는지를 논리값으로 반환한다.
1 | boolean b2 = "안녕하세요" .startsWith( "안녕" );
|
● String substring(int beginIndex[, int endIndex]) : 지정한 위치의 문자열을 잘라낸다.
※ index의 위치에 주의할 것
1 | System.out.println(( "abcdefg" .substring( 0 , 2 )));
|
● String replaceAll(String regex, String replacement) : 특정 문자열을 찾아 다른 문자열로 바꾼다.
1 2 | bigo = bigo.replaceAll( "\n" , "<br/>" );
bigo = bigo.replaceAll( " " , " " );
|
● String trim() : 문자열의 좌우 공백을 제거
1 2 | System.out.printf( " 하이\t*" .trim());
|
● boolean equalsIgnoreCase(String anotherString) : 대소문자 구분 없이 문자열 비교
1 2 | String a = "ABC" , b = "abc" ;
boolean result = a.equalsIgnoreCase(b);
|
● boolean startsWith(String prefix[, int toffset]) : 특정 문자열로 시작하는지를 검색한다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <%
Cookie[] cc = request.getCookies();
if (cc != null ) {
for (Cookie c : cc) {
String name = c.getName();
String value = URLDecoder.decode(c.getValue(), "utf-8" );
if (name.startsWith( "name" )) {
out.print(value + "<br/>" );
}
}
}
%>
|
● public static String format([Locale l,] String format, Object... args)
※ 형식문자열을 지원하는 메서드. 다른 페이지에서 다시 다루기로 한다.
%04d 의 의미
% - 명령의시작
0 - 채워질 문자
4 - 총 자리수
d - 십진정수
%10d, %-10d : 고정출력폭, 여백, -의 경우 오른쪽여백
%.5f, %.1f : 지정한 길이를 초과한 소숫점의 값을 반올림
%6.1f : 6칸의 여백을 확보한 뒤 소숫점 1자리까지 출력
%,d : 1000자리마다 쉼표 출력
%,2.1f : 응용
% [인자 번호] [플래그] [너비] [.정밀도] 유형
%d 십진정수, %f 부동소수점, %x 16진수, %c 문자
%tc 날짜와 시간 전부 표시
%tr 시간만 표시할때
%A %B %C 요일,월,일 표시
String s = String.format("%, d", 1000000000);
System.out.println(s);
String fo = String.format("I have %.2f bugs to fix.", 44444.444);
System.out.println(fo);
String fo1 = String.format("I have %,.2f bugs to fix ", 2231323.23132);
System.out.println(fo1);
String fo2 = String.format("%,6.1f", 1323131123.133123213);
System.out.println(fo2);
String fo3 = String.format("%tc", new Date() );
System.out.println(fo3);
String fo4 = String.format("%tr", new Date() );
System.out.println(fo4);
String fo5 = String.format("%tA %tB %tC", new Date(), new Date(), new Date() );
System.out.println(fo5); // 인자 하나로
String fo6 = String.format("%tA %<tB %<tC", new Date() );
System.out.println(fo6);
------------------------------------------------------------------------
I have 44444.44 bugs to fix. //s
I have 2,231,323.23 bugs to fix //fo
1,323,131,123.1 //fo1
금 9월 05 10:55:14 KST 2008 //fo2
10:55:14 오전 //fo3
금요일 9월 20 //fo4
금요일 9월 20 // fo5