코드:

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
31
32
33
34
35
36
37
38
39
40
41
 
public class String_4 {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String str = "sring compare is complete";
        String key = new String("complete!");
        int result;
        result = key.compareTo(str);
        System.out.println("result = " + result);
 
        String s1 = "java";
        String s2 = "java";
        String s3 = "JAVA";
        String s4 = "good";
        System.out.println(s1.equals(s2));
        System.out.println(s1.equals(s3));
        System.out.println(s1.equals(s4));
        System.out.println(s1.equalsIgnoreCase(s3));
        
        String s11 = "java";
        String s22 = new String(s11);
        System.out.println(s1.equals(s22));
        System.out.println(s11 == s22);
        
        String a[];
        String s = "This    is   a   String";
        a = s.split(" +");
        String s111;
        s111 = s.toLowerCase();
        System.out.println(s111);
        s111 = s.toUpperCase();
        System.out.println(s111);
        s111 = s.trim();
        System.out.println(s111); 
        System.out.println(a.length); 
        
    }
 
}
 
cs

 

실행결과:

설명:

key.compareTo(str)

compareTo()함수를 이용해서 key와 str을 비교. 아스키 코드상으로 차이나는 만큼의 값을 반환.

 

System.out.println(s1.equals(s2));

equals()함수를 통해서 두 String의 값이 같은지 반환. s1 == s2 이런식으로 하면 안됨. 

 

= s.split(" +");

공백을 이용하여 단어들을 따로 구분지음. +를 써주면 공백이 여러번 이어져도 하나의 공백으로 취급

 

s111 = s.toLowerCase();

System.out.println(s111);

s111 = s.toUpperCase();

System.out.println(s111);

String을 대문자 또는 소문자로 바꿈.

 

s111 = s.trim();

맨 앞과 맨 뒤의 공백 제거 함수.

 

 

 

 

'Java' 카테고리의 다른 글

Java, 클래스 생성  (0) 2019.10.04
Java, String(1)  (0) 2019.10.04
Java, Interger.parseInt(), 예외처리  (0) 2019.09.25
Java, Array  (0) 2019.09.25
Java, for-while, continue, Scanner  (0) 2019.09.25

+ Recent posts