소스상에서 문자열리터럴은 String 객체로 자동 생성되지만, String 클래스의 다양한 생성자를 이용해서 직접 String 객체를 생성할 수도 있습니다.
어떤 생성자를 이용해서 String 객체를 생성할지는 제공되는 매개값의 타입에 달려 있습니다.
//배열 전체를 String 객체로 생성
String str = new String(byte[] bytes)
//지정한 문자셋으로 디코딩
String str = new String(byte[] bytes, String charseName);
//배열의 offset 인덱스 위치부터 length만큼 String 객체로 생성
String str = new String(byte[] bytes, int offset, int length);
//지정한 문자셋으로 디코딩
String str = new String(byte[] bytes, int offset, int length, String charseName)
public class ByteRoStringExample {
public static void main(String[] args) {
byte[] bytes = {72, 101, 108, 108, 111, 32, 74, 97, 118, 97};
String str1 = new String(bytes);
System.out.println(str1);
// 6은 74의 인덱스 4는 4개(74 97 118 97)
String str2 = new String(bytes, 6, 4);
System.out.println(str2);
}
}
결과
Hello Java
Java

문자 추출(charAt())
문자열 비교 (equals())
바이트 배열로 변환(getBytes())
문자열 찾기(indexOf(), 번외:contains())
문자열 길이 (length())
문자열 대치 (replace())