본문 바로가기
JAVA

4일차// 대표적인 String 메소드, / 문자열을 자른다 .split/ 문자열의 범위로 자르는 함수 substring

by aesup 2021. 1. 14.
728x90
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
     // 문자열을 자른다.
    // split
    
    /*
     
     aaaa-bbbb-ccccc
     aaaa
     bbb
     ccccc// 이것만 때서 사용하고 싶을때!
     
     홍길동-24-2001/12/23-서울시
     안신 - 21-2001/10/11
     
     */
    String str8 = "홍길동-24-2001/12/23-서울시-181.1";
    String split[] =str8.split("-");
    
    System.out.println(split.length);   // 5             출력
    System.out.println(split[0]);        // 홍길동
    System.out.println(split[1]);        // 24
    System.out.println(split[2]);        // 2001/12/23
    System.out.println(split[3]);        // 서울시
    System.out.println(split[4]);        // 181.1
    
    
 
    // 문자열의 범위로 자르는 함수
    // substring
    
    String str9 = "안녕 반가워요 건강해요";
    String subStr = str9.substring(02); //시작위치, 이 위치 전까지  // 안녕 출력
    
    System.out.println("str9 = "+ subStr);
    
    String subStr2 = str9.substring(36); // 반가워 
    System.out.println("str9 = "+ subStr2);
    
 
cs

변수.split( 해당 문자열에서 자를 기준 char);

 

변수. substring( 자르기위한 시작 위치, 이 위치 전까지);

 

728x90