public class Example {
public static void main(String[] args) {
String subject = "자바프로그래밍";
char ch1 = subject.charAt(1);
char ch2 = "abcd".charAt(0);
// "abcd"로만 써도 compiler가 String 객체로 반환한다...!
System.out.println(ch1); // "바" 출력
System.out.println(ch2); // "a" 출력
}
}
: 각각의 String 객체를 생성해주므로 적혀진 글자가 동일하더라도 각가의 변수는 다른 주소값을 가짐 -> 문자열에 담긴 문자가 동일한지를 비교하려면 Object 클래스의 equals()라는 메소드를 사용하는 것이 좋음
: String 리터럴을 변수에 바로 넣어주므로 글자가 동일한 변수는 동일한 주소 값을 가짐
이때, 리터럴은 Method Area안의 상수 pool이라는 곳에 올라가서 공유됨
// 리터럴을 정의
String s1 = "Java";
String s2 = "Java";
// 객체를 생성
String s3 = new String("Java");
String s4 = new String("Java");
if (s1 == s2){
System.out.println("같습니다");
} else{
System.out.println("다릅니다");
} //같습니다가 출력
if (s3 == s4){
System.out.println("같습니다");
} else{
System.out.println("다릅니다");
}
// 다릅니다가 출력
//서로 다른 객체가 생성되어 주소값이 다름
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String aString = (String)anObject;
if (coder() == aString.coder()) {
return isLatin1() ? StringLatin1.equals(value, aString.value)
: StringUTF16.equals(value, aString.value);
}
}
return false;
}
// coder의 소스코드
String(byte[] value, byte coder) {
this.value = value;
this.coder = coder;
}
byte coder() {
return COMPACT_STRINGS ? coder : UTF16;
}
String s1 = "java";
s1 = "python";
s1 = "c++";
이라고 한 경우 java와 python, c++가 모두 상수 pool에 올라간다
(1) 인덱스는 0부터 시작한다!
(2) 빈 공간도 하나의 문자이다.: 아스키코드 32번
public class Example {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char ch1 = sc.next().charAt(0);
System.out.println(ch1);
}
"안녕하세요"를 넣어주는 경우 첫번째 글자인 "안"을 출력한다
(1) indexOf와 매칭되는 단어의 첫번째 인덱스를 출력한다
(2) 단어가 여러개 있는 경우 가장 앞에 있는 단어의 인덱스를 출력한다
(3) 매칭되지 않으면 -1을 출력한다.
public class Example {
public static void main(String[] args) {
String str3 = "the cat is on the table";
int index1 = str3.indexOf("the");
int index2 = str3.indexOf("rain");
if (index1 == -1){
System.out.println("찾는 문자열이 없습니다");
} else{
System.out.println(index1);
}// 0출력
if (index2 == -1){
System.out.println("찾는 문자열이 없습니다");
} else{
System.out.println(index2);
}// -1 출력
}
}
public class Example0929_5 {
public static void main(String[] args) {
String trimStr = " I Love you . ";
System.out.println(trimStr);
System.out.println(trimStr.length());
System.out.println(trimStr.trim());
System.out.println(trimStr.trim().length());
}
}
(1) 파이썬의 split()과 동일하게 split안에 넣어준 문자열(ex. ",", ".", " " 등)을 기준으로 해당 문자열을 잘라준다.
(2) split은 String[]으로 return된다.
- public class Example0929_5 {
public static void main(String[] args) {
String[] tokens = "I am a boy".split(" ");
//split()은 String[]로 return 된다!
for (String token: tokens) {
System.out.println(token);
}
}
(1) 파라미터에 double, int, boolean 등등 다양한 타입이 들어와도 string으로 변환: 함수 오버로딩을 이용함
String str1 = String.valueOf(10.00);
String str2 = String.valueOf(false);
System.out.println(str1);
System.out.println(str2);
(2) print 함수도 valueOf() 메소드를 이용하여 문자열의 형태로 출력함
System.out.print(str);
// print의 소스코드
public void print(String s) {
write(String.valueOf(s));
}
String str44 = "100";
int num2 = Integer.parseInt(str44);
System.out.println(num2);