- 주어진 데이터를 String으로 변환하는 클래스. StringBuffer와 비슷한 역할을 하지만 동기화를 지원하지 않는다. 따라서 멀티스레드에서 사용될 때는 성능을 보장할 수 없다.
- StringBuffer에 비해 싱글스레드에서 속도가 더 빠르다는 특징이 있다.
- 기본적으로 16자의 제한 용량이 있다. 그렇지만 그 용량이 넘치더라도 자동으로 용량을 키워 준다.
- 주요 기능으로 append, insert가 있다. 둘 모두에서 character 배열은 2, 3번째 파라미터로 offset, length를, CharSequence는 start, end를 설정하여 substring을 따로 넘겨줄 수 있다.
생성자
- StringBuilder() : 16개의 character를 담을 수 있는 빈 string builder 생성
- StringBuilder(int capacity) : 파라미터를 통해 용량을 설정한 빈 string builder 생성
- StringBuilder(String string) : 전달된 string으로 string builder 생성. capacity는 16 + string의 길이가 된다.
- StringBuilder(CharSequence charSequence) : 전달된 charSequence로 string builder 생성
메소드
StringBuilder Type
- append(AnyType anything) : 어떤 것이든 추가
- appendCodePoint(int codePoint) : 해당 아스키코드의 character를 추가
- insert(int offset, AnyType anything) : 자유롭게 삽입
- delete(int start, int end) : start, end로 특정 범위 삭제
- deleteCharAt(int index) : index로 특정 인덱스 삭제
- replace(int start, int end, String string) : 지정된 영역을 새로운 string으로 변경
- reverse() : 순서 거꾸로 변환
int Type
- capacity() : 용량 리턴
- length() : 길이 리턴
- codePointAt(int index) : 해당 인덱스에 있는 character의 아스키코드 리턴
- codePointBefore(int index) : 해당 인덱스 바로 앞 character의 아스키코드 리턴
- codePointCount(int start, int end)
- indexOf(String string)
- indexOf(String string, int fromIndex)
- lastIndexOf(String string)
- lastIndexOf(String string, int fromIndex)
- offsetByCodePoints(int index, int codePointOffset)
void Type
- setCharAt(int index, char character) : 해당 인덱스를 주어진 character로 변경
- setLength(int newLength) : 새로운 길이 설정
- ensureCapacity(int minimumCapacity) : 최소 용량 설정
- trimToSize() : 문자열의 길이만큼만 길이를 남김
- getChars(int sourceBegin, int sourceEnd, char[] destination, int destinationBegin)
String Type
- toString() : String으로 변환
- subString(int start) : 시작 index로부터 서브스트링 생성
- subString(int start, int end) : 주어진 범위로 서브스트링 생성
char Type
- charAt(int index) : index에 있는 character 리턴
CharSequence Type
- subSequence(int start, int end) : 해당 범위로 새로운 character sequence 생성
'Java' 카테고리의 다른 글
jackson의 두 라이브러리, codehaus vs fasterxml (0) | 2022.03.14 |
---|---|
AutoBoxing과 AutoUnboxing (0) | 2022.03.14 |
ArrayList와 LinkedList, 어떤 것을 사용해야 할까? (0) | 2022.03.14 |
Java에서의 여러가지 정렬(Sorting) (0) | 2022.03.14 |
Java Custom Annotation (자바 커스텀 어노테이션 만들기) (0) | 2021.07.09 |