반응형


length() :: 문자열의 길이를 알려주는 함수이다.


startsWith() :: 문자열의 시작이 특정 문자열과 일치하는지 확인시켜주는 함수이다.


endsWith() :: 문자열의 끝이 특정 문자열과 일치하는지 확인시켜주는 함수이다.


indexOf() :: 특정 문자열의 위치를 찾아주는 함수이다. 이때 처음으로 나오는 위치를 알려준다.


lastIndexof() :: 특정 문자열의 위치를 찾아주는 함수이다. 이때 마지막으로 나오는 위치를 알려준다.


replace() :: 문자열을 바꿔준다.


substring() :: a번부터 b번까지 문자열을 추출해준다.


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
42
43
44
45
46
package JavaBasic;
 
public class Jmain {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
 
        String str = "hello world";
        
        // 문자열의 길이를 알려주는 length()
        System.out.println("문자열 길이 :: " + str.length());
        
        // 문자열의 처음이 특정 문자열인지 확인하는 startsWith()
        // 문자가 일치한다면 true를 반환, 아니면 false를 반환
        boolean ret = str.startsWith("h");
        System.out.println("ret 값 :: " + ret);
        
        // 문자열의 끝이 특정 문자열인지 확인하는 startsWith()
        // 문자가 일치한다면 true를 반환, 아니면 false를 반환
        ret = str.endsWith("a");
        System.out.println("ret 값 :: " + ret);
        
        // 특정 문자열의 위치를 찾아주는 indexOf() (처음 나오는 위치를 알려준다.)
        int pos = str.indexOf("o");
        System.out.println("o가 처음 나오는 위치 :: " + pos);
        
        // 특정 문자열의 위치를 찾아주는 lastIndexOf() (마지막에 나오는 위치를 알려준다.)
        pos = str.lastIndexOf("o");
        System.out.println("o가 마지막에 나오는 위치 :: " + pos);
        
        // 문자열을 바꿔주는 replace()
        System.out.println("이전 str :: " + str);
        String str1 = str.replace("hello""안녕");
        System.out.println("바뀐 str1 :: " + str1);
        
        // 일부 문자열을 추출해주는 substring()
        System.out.println("6~11번 위치 str 출력 :: " + str.substring(611));
 
    
    }
 
}
 
//                                                       This source code Copyright belongs to Crocus
//                                                        If you want to see more? click here >>
 
Crocus






반응형