java.lang.String ํด๋์ค๋ ๋ฌธ์์ด ์ฒ๋ฆฌ๋ฅผ ์ํ ํด๋์ค๋ก,
String ํด๋์ค๋ก ์์ฑ๋ ๋ฌธ์์ด์ ํ ๋ฒ ์์ฑ๋๋ฉด ๋ณ๊ฒฝํ ์ ์๋ ๋ฌธ์์ด์ด๋ค.
String str1 = "JAVA";
String str2 = "JAVA";
//๋ฉ๋ชจ๋ฆฌ str1/str2 -> JAVA ๊ฐ๋ฅดํด
String str3 = new String("JAVA");
String str4 = new String("JAVA");
// ๋ฉ๋ชจ๋ฆฌ
str3 -> JAVA
str4 -> JAVA
class EqualsExam{
public static void main(String args[]){
String str1 = "Love";
String str2 = "Love";
String str3 = new String("LoveYou");
String str4 = new String("LoveYou");
if(str1 == str2)
System.out.println("์ฃผ์ ๊ฐ๋ค.");
else
System.out.println("์ฃผ์ ๋ค๋ฅด๋ค.");
if(str3 == str4)
System.out.println("์ฃผ์ ๊ฐ๋ค.");
else
System.out.println("์ฃผ์ ๋ค๋ฅด๋ค.");
if(str1.equals(str2))
System.out.println("๋ด์ฉ ๊ฐ๋ค.");
else
System.out.println("๋ด์ฉ ๋ค๋ฅด๋ค");
if(str3.equals(str4))
System.out.println("๋ด์ฉ ๊ฐ๋ค.");
else
System.out.println("๋ด์ฉ ๋ค๋ฅด๋ค");
}
}
์คํ๊ฒฐ๊ณผ
์ฃผ์๊ฐ๋ค
//str1 == str2 ๋ฌธ์์ด ์์๋ฅผ ๋ง๋ค์ด์ ๋น๊ตํจ์ผ๋ก T
์ฃผ์ ๋ค๋ฅด๋ค
//str3 == str4 String ํด๋์ค ์์ฑ์๋ฅผ ๋ง๋ค์ด์ ๋น๊ต F
๋ด์ฉ ๊ฐ๋ค
//str1.equals(str2) ๋ด์ฉ์ด ๊ฐ์ผ๋ฏ๋ก T
๋ด์ฉ ๊ฐ๋ค
//str3.equals(str4) ๋ด์ฉ์ด ๊ฐ์ผ๋ฏ๋ก T
class LengthExam{
public static void main(String args[]){
String strData1 = "I Love You";
System.out.println("strData1.length() = " + strData1.length());
}
}
์คํ๊ฒฐ๊ณผ
strData1.length() = 10
//๊ณต๋ฐฑ๋ ํ๋์ ๊ธ์๋ก ์ธ์ํ๋ค.
๋ฌธ์์ด์ ํน์ ์์น์ ๋ฌธ์๋ฅผ ๋ฐํํ๋ ๋ฉ์๋์ด๋ค.
ํ์
public char charAt(int index) //index๋ฒ์ : 0 ~ length-1
์์
class CharAtExam{
public static void main(String args[]){
String str1 = "I Love Korea";
for(int i = 0; i < str1.length(); i++){
System.out.print(str1.charAt(i));
}
System.out.println();
}
}
์คํ๊ฒฐ๊ณผ
I Love Korea
๋ฌธ์์ด์ ๋ด์ฉ์ด ๊ฐ์์ง๋ฅผ ๋น๊ตํ๋ ๋ฉ์๋๋ก ๊ฐ์ฒด ๋ด์ ๋ฌธ์์ด์ด ๊ฐ์ผ๋ฉด true๋ฅผ ๋ฐํํ๊ณ ๊ฑฐ์ง์ด๋ฉด false๋ฅผ ๋ฐํํ๋ ๋ฉ์๋
ํ์
public boolean equals(Object obj)
์์
class StringEqualExam{
public static void main(String args[]){
String R1 = new String("JAVA");
String R2 = new String("JAVA");
Stirng R3 = new String("java");
System.out.println("R1 == R2 -->" + (R1 == R2));
System.out.println("R1.equals(R2) -> " + (R1.equals(R2));
System.out.println("R1 == R3 -->" + (R1 == R3));
System.out.println("R1.equals(R3) -> " + R1.equals(R3));
}
}
์คํ๊ฒฐ๊ณผ
R1 == R2 -> false
R1.equals(R2) -> true
R1 == R3 -> false
R1.equals(R3) -> false //๋์๋ฌธ์ ๊ตฌ๋ถ
๋งค๊ฐ๋ณ์์ ๊ฐ์ String ํ์ผ๋ก ๋ณํํ๋ ๊ฒ
ํ์
public static String valueOf(๋งค๊ฐ๋ณ์)
๋งค๊ฐ๋ณ์์๋ boolean, char , char[], double, float, int, long, Object ๋ฑ์ด ์ฌ ์ ์๋ค.
์์
class ValueOfExam{
public static void main(String args[]){
char ch = 'A';
String strData1 = "A";
String strData2;
strData2 = String.valueOf(ch);
if(strData1.equals(strData2))
System.out.println("๊ฐ๋ค");
else
System.out.println("๋ค๋ฆ");
}
}
๊ฒฐ๊ณผ
๊ฐ์
// char ์ Stringํ์ผ๋ก ๋ณํํด์ฃผ์ด "A","A" ๊ฐ์ผ๋ฏ๋ก T
public String trim()
์์
class TrimExam{
public static void main(String args[]){
String strData1 = " I Love You ";
String strData2;
strData2 = strData1.trim();
System.out.println("strData2.tri() ->" + strData2);
if(strData1.equals(strData2))
System.out.println("๊ฐ๋ค.");
else
System.out.println("๋ค๋ฅด๋ค");
}
}
์คํ๊ฒฐ๊ณผ
strData2.trim() -> I Love You
๋ค๋ฅด๋ค. //๊ณต๋ฐฑ์ด ์ง์ ์ก๊ธฐ ๋๋ฌธ
public String subString(int start)
public String subString(int start, int end)
์์
class SubStringExam{
public static void main(String args[]){
Strng strData1 = "I Love You";
String strData3;
strData3 = strData1.subString(7);
System.out.println("strData3.subString(7) -> "+ strData3);
strData3 = strData1.subString(2,5);
System.out.println("strData3.subString(2,5) -> " + strData3);
}
}
์คํ๊ฒฐ๊ณผ
strData3.subString(7) -> You
strData3.susbString(2,5) -> Lov
Have you played Stickman Boost? overcoming thousands of obstacles to get the present like the way we live everyday. Come to visit and play to have funny time and get superb adventures.