728x90


String 


  int length() : 문자열 길이 구함..  필드 length와 혼동주의!

  boolean equals(Object obj) : String 클래스 비교
  boolean equalsIgnoreCase(Object obj) : 대소문자 구문 않고 비교

  String substring(int start) : start부터 끝까지 문자열 발췌 subString으로 혼동주의!
  String substring(int start, int end) : start부터 end 직전까지 문자열 발췌

  String concat(String str) : 문자열 결합
  String replace(char old, char new) : old문자을 new문자로 치환

  String toLowerCase() : 소문자로 변환
  String toUpperCase() : 대문자로 변환

  char charAt(int index) : index번째 문자 반환


class StringTest_1
{
 public  static void main(String args[])
 {
  String s1 = "Hello";
  String s2 = "Good";
  System.out.println(s1.length());
  System.out.println(s1.concat(s2)); // s1과s2를 합침
  
  System.out.println(s1.replace('e','a'));
  
  System.out.println(s1.toLowerCase());
  System.out.println(s2.toUpperCase());
  
  String str1 = new String("이름 홍길동");
  System.out.println(str1);
  System.out.println(str1.replace("홍길동","저팔계"));
 }
}



String 비교
같은 내용의 문자열 상수를 하나의 프로그램 소수에서
두번이상 사용하게 되면 한번만 메모리에 할당하고 이를 공유한다.
String str = "hello";

new String()으로 객체 생성하게되면 별도의 기억 공간을 확보한다.
String str = new String("hello");



class StringTest_2
{
 public static void main( String [] args )
 {
  String s1 = "Good";  // s1과 s2 주소가 같다. 왜냐면 문자열 상수이기 때문에... 메모리 절약 차원에서
  String s2 = "Good";
  
  if(s1 == s2)  
  {
   System.out.println("같다");
  }else
  {
   System.out.println("틀리다");
  }
  
  String s3 = new String("hello");  //s1과 s2 주소가 다르다 왜나면  동적할당 했으므로.
  String s4 = new String("hello");
  
  if(s3 == s4)
  {
   System.out.println("같다");
  }else
  {
   System.out.println("틀리다");
  }
 }// end main

}



class StringTest_3
{
 public static void main( String [] args )
 {
  String s1 = new String("Hello");
  String s2 = new String("Hello");
  
  System.out.println("-String형의 레퍼런스 변수가 참조하는 인스턴스가 같은지 비교-");
  
  if(s1 == s2) // 객체가 가리키는 주소값 비교
  {
   System.out.println("같다");
  }else
  {
   System.out.println("같지않다");
  }
  
  System.out.println("- String형의 데이터값(내용) 비교 -");
  
  if(s1.equals(s2)) //객체에 들어있는 내용 비교,  String 클래스 객체는 객체 자체가 문자열이다.
  {
   System.out.println("같다");
  }else
  {
   System.out.println("같지않다.");
  }
 }// end main
}

+ Recent posts