카테고리

분류 전체보기 (68)
JAVA-기본 (7)
JAVA-AWT (24)
JAVA-클래스의 중요메소드 (23)
JAVA-람다식 (0)
Android Studio (7)
Python (1)
OpenCV (1)
AWS (0)
webrtc (0)
javascript (4)
처리방침 (1)
rss 아이콘 이미지
반응형

아래는 String 클래스의 메소드들입니다. 그 중에서 가장 많이 사용하는 메소들을 정리하였으며 주요 메소들을 설명 드리도록 하겠습니다. 

String 클래스는 문자열 데이터값을 저장하는 객체로 사용됩니다. 또한, 문자열은 변경될 수 없습니다.


boolean equals
(Object anObject)
Compares this string to the specified object.
     
int length() Returns the length of this string.
     
int indexOf
(int ch)
Returns the index within this string of the first occurrence of the specified character.
     
char charAt(int index) Returns the char value at the specified index.
     
string replace
(char oldChar, char newChar)
Replaces each substring of this string that matches the literal
target sequence with the specified literal replacement sequence.
     
string substring
(int beginIndex)
Returns a string that is a substring of this string.
     
string substring
(int beginIndex, int endIndex)
Returns a string that is a substring of this string.
     
string toString() This object (which is already a string!) is itself returned.
     
string toLowerCase() Converts all of the characters in this String to lower case using the rules of the default locale.
     
string toUpperCase() Converts all of the characters in this String to upper case using the rules of the default locale.
     
string trim() Returns a string whose value is this string, with any leading and trailing whitespace removed.
     
string concat(String str) Concatenates the specified string to the end of this string.
     
sring[] split(String regex) Splits this string around matches of the given regular expression.


다음 강의에서 위의 String클래스의 메소드들을 확인해보도록 하겠습니다.

아주 유용한 메소들로 많이 사용되니 상세히 파악해보도록 하겠습니다.

다음 강의를 참고해주세요.


자료가 마음에 드셨다면 자주 찾아주세요^^ 글 올리는데 힘이됩니다.


반응형

'JAVA-클래스의 중요메소드 > String클래스' 카테고리의 다른 글

JAVA-중요06-replace()  (0) 2018.06.29
JAVA-중요05-lastIndexOf()  (0) 2018.06.28
JAVA-중요04-indexOf()  (0) 2018.06.28
JAVA-중요03-charAt()  (0) 2018.06.28
JAVA-중요02-length()/length  (0) 2018.06.21
반응형

앞의 강의에서 toString에 대해서 확인하였습니다. 그렇다면 다음 예제를 통하여 toString에 대해서 알아보도록 하겠습니다.


toString을 통하여 내가 원하는 양식으로 출력이 가능합니다.

아주 좋은 메소드입니다.


1번 예제를 살펴보도록 하겠습니다.

 package 기본기03;

 

public class T4 {

 

        public static void main(String[] args) {

 

               Test t1 = new Test("홍길동", 1);

               Test t2 = new Test("고길동", 20);

               Test t3 = new Test("둘리", 30);

               Test t4 = new Test("마이콜", 41);

               System.out.println(t1);

               System.out.println(t2);

               System.out.println(t3);

               System.out.println(t4);

 

       

        }

}

 

class Test {

        String name;

        int age;

 

        public Test(String name, int age) {

               //위의 형태는 (괄호안의 양식이 클래스의 입력으로 들어간다는 말입니다.)

               //위쪽 부분의 Test t1 = new Test("홍길동", 1); 보시면 string 홍길동/ int 1 들어갑니다.

               this.name = name;

               this.age = age;

        }

 

        @Override

        public String toString() {

               return "내가 원하는 양식입니다                이름:" + name + "         나이는 :" + age + "";

               //내가 원하는 양식으로 만들 있습니다.

        }

}


<결과물>




2번 예제를 살펴보도록 하겠습니다.

 package 기본기03;

 

public class T4 {

 

        public static void main(String[] args) {

 

              

               Test t1 = new Test("홍길동",11,3,2);

               Test t2 = new Test("고길동",10,2,1);

               Test t3 = new Test("김길동",12,4,2);

               Test t4 = new Test("추길동",13,5,6);

               Test t5 = new Test("구길동",14,6,1);

              

               System.out.println(t1);

               System.out.println(t2);

               System.out.println(t3);

               System.out.println(t4);

               //학사관리 시스템에 이용

       

        }

}

 

class Test {

        String name;

        int age;

        int grade;

        int ban;

       

        public Test(String name, int age, int grade, int ban) {

               //위의 형태는 (괄호안의 양식이 클래스의 입력으로 들어간다는 말입니다.)

               

               this.name = name;

               this.age = age;

               this.grade=grade;

               this.ban=ban;

               }

 

        @Override

        public String toString() {

               return "이름:"+name+" 나이:"+age+" "+grade+"학년 "+ban+"";

               //내가 원하는 양식으로 만들 있습니다.

        }

}


<결과물>


자료가 마음에 드셨다면 자주 찾아주세요^^ 글 올리는데 힘이됩니다.


반응형

'JAVA-클래스의 중요메소드 > Object클래스' 카테고리의 다른 글

JAVA-중요02-toString  (0) 2018.06.21
JAVA-중요01-equals(예제)  (0) 2018.06.21
JAVA-중요01-equals  (0) 2018.06.21
반응형

아래는 Object클래스의 메소드들입니다. 그 중에서 가장 많이 사용하는 메소들을 골라서 설명 드리도록 하겠습니다.


toString();

toString은 현재 객체를 문자열 형태로 리턴하여 주는 메소드입니다. 아래는 toString의 특징입니다.

1. 객체를 출력하는 System.out.println/ System.out.print 메소드는 toString을 호출합니다.

(Object 클래스는 모든 클래스의 부모역할을 하므로 모든 객체는 이를 상속받는다.)


2. toString은 클래스이름@객체의코드를 16진수의 값으로 표현합니다.


 package 기본기03;

 

public class T4 {

 

        public static void main(String[] args) {

 

               Test t1 = new Test();

               System.out.println(t1);

               //클래스이름@객체의 코드를 나타내는 16진수의 표현

        }

}

 

class Test {

        String name;

        int age;

}


<결과물>

위의 결과물은 클래스이름@객체의 코드를 16진수의 값으로 나태내고 있습니다.



다음은 toString을 override한 예제입니다.

override 방법은 마우스오른쪽 보튼-source-Gemerate toString()을 클릭합니다.

 package 기본기03;

 

public class T4 {

 

        public static void main(String[] args) {

 

               Test t1 = new Test();

               System.out.println(t1);

               //클래스이름@객체의 코드를 나타내는 16진수의 표현

        }

}

 

class Test {

        String name;

        int age;

 

        @Override

        public String toString() {

               return "Test [name=" + name + ", age=" + age + "]";

 

        }

}


<결과물>

그렇다면 toString을 배웠으니 다음 강의에서 예제를 통해서 원하는 양식을 만들어 보도록하겠습니다.



자료가 마음에 드셨다면 자주 찾아주세요^^ 글 올리는데 힘이됩니다.



반응형

'JAVA-클래스의 중요메소드 > Object클래스' 카테고리의 다른 글

JAVA-중요02-toString(예제)  (0) 2018.06.21
JAVA-중요01-equals(예제)  (0) 2018.06.21
JAVA-중요01-equals  (0) 2018.06.21