달력

62025  이전 다음

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30


// String 클래스 연습

class StringEx2
{
 public static void main( String[] args )
 {
  String str = "Hello Java!!!";

  // char charAt ( int index )
  System.out.println( "charAt : " + str.charAt( 6 ) );

  // int indexOf ( int ch )
  System.out.println( "indexOf : " + str.indexOf( 'J' ) );

  // int indexOf ( int ch, int fromIndex )
  System.out.println( "indexOf : " + str.indexOf( 'a', 8 ) );    // 8번째부터 찾아라 ( 시작위치를 정한다. )

  // int indexOf ( String str )
  System.out.println( "indexOf : " + str.indexOf( "!!" ) );

  // lastIndexOf ( int ch )
  System.out.println( "lastIndexOf : " + str.lastIndexOf( 'a' ) );

  // String concat ( String str )
  System.out.println( "concat : " + str.concat( "!!" ) );      // 공간을 새로 만들기 때문에 원본이나 !!가 가비지가 된다.
  System.out.println( "concat : " + str );

  // int length()
  System.out.println( "length : " + str.length() );        // 널을 빼고 계산해서 글자수는 13개

  // String substring ( int beginIndex )
  System.out.println( "substring : " + str.substring( 6 ) );

  // String substring ( int beginIndex, int endIndex )      // endIndex - 1
  System.out.println( "substring : " + str.substring( 6, 12 ) );

  // String trim ()
  String s = "   a a  a   ";
  System.out.println( "trim = " + s );
  System.out.println( "trim = " + s.trim() );


 }
}

Posted by cdprkr2077
|