Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
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
Tags
more
Archives
Today
Total
관리 메뉴

WON.dev

chapter05 : String 본문

JAVA/chapter20_api

chapter05 : String

GAWON 2023. 5. 30. 18:19
package org.joonzis.ex;

public class Ex05_String {
	public static void main(String[] args) {
		
		// 인스턴스는 다르지만 내부 데이터가 동일하므로 객체 1개만 저장
		String a = "apple";
		String b = "apple";
		System.out.println(a == b ? "apple 1개" : "apple 2개");
		
		String c = new String("banana");
		String d = new String("banana");
		System.out.println(c == d ? "banana 1개" : "banana 2개");
		
		// .split()
		String sn = "000000-1234567";
		String[] snArr = sn.split("-");	// 하이픈(-)으로 분리하여 각각 배열에 저장
		for(String s : snArr) {
			System.out.println(s);
		}
		
		String today = "1980.10.21";
		String[] ymd = today.split("\\.");
		// 몇몇 특수문자는 사용 시 앞에 역슬래쉬2개(\\)를 붙여줘야 함
		//  |  ?  *  (  )  {  }  [  ]  \
		for(String s : ymd) {
			System.out.println(s);
		}
		
		
		// .join()
		String today2 = String.join("-", ymd);	// 배열 요소 사이에 하이픈(-) 넣어서 합치기
		System.out.println(today2);
		
		
		// .valueOf
		// 정수 -> 문자열 : String.valueOf(10) == "10"
		// 실수 -> 문자열 : String.valueOf(1.5) == "1.5"
		
		// .substring()
		// .substring(시작인덱스) : 시작 부터 끝까지 출력
		// .substring(시작인덱스, 종료인덱스) : 시작 인덱스 포함, 종료 인덱스 불포함(종료 인덱스 전까지)
		
		String phone = "010-1234-5678";
		
		String p1 = phone.substring(0, 3);
		String p2 = phone.substring(4, 8);
		String p3 = phone.substring(9);
		
		System.out.println(p1);
		System.out.println(p2);
		System.out.println(p3);
		
		
		
	}
}

'JAVA > chapter20_api' 카테고리의 다른 글

chapter07 : Wrapper  (0) 2023.05.30
chapter06 : StringBuffer  (0) 2023.05.30
chapter04 : System  (0) 2023.05.30
chapter03 : Object_clone  (0) 2023.05.30
chapter02 : Object_equals  (0) 2023.05.30