๐Ÿฆ[JAVA] String ํด๋ž˜์Šค๐Ÿฆ

Kim-yujinยท2022๋…„ 4์›” 18์ผ
0

์ž๋ฐ”

๋ชฉ๋ก ๋ณด๊ธฐ
10/15
post-thumbnail

String ํด๋ž˜์Šค

java.lang.String ํด๋ž˜์Šค๋Š” ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ํด๋ž˜์Šค๋กœ,
String ํด๋ž˜์Šค๋กœ ์ƒ์„ฑ๋œ ๋ฌธ์ž์—ด์€ ํ•œ ๋ฒˆ ์ƒ์„ฑ๋˜๋ฉด ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋Š” ๋ฌธ์ž์—ด์ด๋‹ค.

๊ฐ€. ๊ฐ์ฒด ์ƒ์„ฑ ๋ฐฉ๋ฒ•

(1) ๋ฌธ์ž์—ด ์ƒ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

  • ์ž๋ฐ” ๊ฐ€์ƒ ๋จธ์‹ ์€ ๋‚ด๋ถ€ ๋ฌธ์ž์—ด ์ €์žฅ์†Œ์— ๊ฐ™์€ ๋‚ด์šฉ์˜ String๊ฐ์ฒด๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์—ฌ ์žˆ์„ ๊ฒฝ์šฐ ํ•ด๋‹น ๊ฐ์ฒด์˜ ๋ž˜ํผ๋Ÿฐ์Šค๋งŒ์„ ๋ฆฌํ„ดํ•œ๋‹ค.
String str1 = "JAVA";
 String str2 = "JAVA";
 
 //๋ฉ”๋ชจ๋ฆฌ str1/str2 -> JAVA ๊ฐ€๋ฅดํ‚ด

(2) Stringํด๋ž˜์Šค ์ƒ์„ฑ์ž๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

  • ๋งค๋ฒˆ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค.
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

๋Œ€ํ‘œ์ ์ธ ๋ฉ”์†Œ๋“œ

๊ฐ€. length()๋ฉ”์†Œ๋“œ

  • Stringํด๋ž˜์Šค์˜ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ์ด๋‹ค.
  • StringBuffer ํด๋ž˜์Šค์˜ length()๋ฉ”์†Œ๋“œ์™€ ๊ฐ™์ด ํด๋ž˜์Šค ๊ฐ์ฒด์— ์ €์žฅ๋œ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ, ์ด๋•Œ ๊ณต๋ฐฑ๋„ ํ•˜๋‚˜์˜ ๋ฌธ์ž๋กœ ์ธ์‹ํ•œ๋‹ค.
class LengthExam{
	public static void main(String args[]){
    	String strData1 = "I Love You";
     System.out.println("strData1.length() = " + strData1.length());
    }
}

์‹คํ–‰๊ฒฐ๊ณผ

strData1.length() = 10
//๊ณต๋ฐฑ๋„ ํ•˜๋‚˜์˜ ๊ธ€์ž๋กœ ์ธ์‹ํ•œ๋‹ค.


๋‚˜. charAt()๋ฉ”์†Œ๋“œ

  • ๋ฌธ์ž์—ด์˜ ํŠน์ • ์œ„์น˜์˜ ๋ฌธ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ์ด๋‹ค.

  • ํ˜•์‹

    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


๋‹ค. equals()๋ฉ”์†Œ๋“œ

  • ๋ฌธ์ž์—ด์˜ ๋‚ด์šฉ์ด ๊ฐ™์€์ง€๋ฅผ ๋น„๊ตํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋กœ ๊ฐ์ฒด ๋‚ด์˜ ๋ฌธ์ž์—ด์ด ๊ฐ™์œผ๋ฉด 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 //๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„


๋ผ. valueOf()๋ฉ”์†Œ๋“œ

  • ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ฐ’์„ 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

๋งˆ. trim() ๋ฉ”์†Œ๋“œ

  • ๋ฌธ์ž์—ด์˜ ์•ž๋’ค ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•˜๋Š” ๋ฉ”์†Œ๋“œ์ด๋‹ค.
  • ํ˜•์‹

    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
๋‹ค๋ฅด๋‹ค. //๊ณต๋ฐฑ์ด ์ง€์› ์กŒ๊ธฐ ๋•Œ๋ฌธ


๋ฐ”. subString()๋ฉ”์†Œ๋“œ

  • ๋ฌธ์ž์—ด์˜ ์ผ๋ถ€๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ
  • ํ˜•์‹

    public String subString(int start)

  • start๋ถ€ํ„ฐ ๋๊นŒ์ง€ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ์ด๋‹ค.
  • ๋ฌธ์ž์—ด์˜ ์œ„์น˜๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค.
  • ํ˜•์‹

    public String subString(int start, int end)

  • start ~ end -1 ๊นŒ์ง€ ๋ฌธ์ž์—ด์„ ๋ฆฌํ„ดํ•˜๋Š” ๋ฉ”์†Œ๋“œ

์˜ˆ์ œ

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

profile
๐Ÿฐ๋…ธ๋ ฅํ•˜๋ฉฐ ์‚ด์•„๊ฐ€๊ธฐ๐Ÿฐ

1๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2022๋…„ 8์›” 24์ผ

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.

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ

๊ด€๋ จ ์ฑ„์šฉ ์ •๋ณด