package demo3;

public class StringDemo1 {
	public static void main(String[] args) {
		
		String str1 = "hello world";
		// String str1 = new String("hello world");
		
		// String toUpperCase() : 소문자를 대문자로 변환해서 반환한다.
		// String toLowerCase() : 대문자를 소문자로 변환해서 반환한다.

		String result1 = str1.toUpperCase();
		
		System.out.println("원본1 : " + str1);
		System.out.println("결과1 : " + result1);
		System.out.println("결과1 : " + str1.toUpperCase());
		
		String result2 = result1.toLowerCase();
		System.out.println("결과2 : " + result2);
		System.out.println(str1);

		str1 = str1 + "a";
		System.out.println(str1);

		
		// String replace(String old, String new) : 해당 글자 모두 바꾸기
		String str2 = "이것이 자바다. 자바 초급자를 위한 안내서";
		String result3 = str2.replace("자바", "파이썬");
		System.out.println("원본2 : " + str2);
		System.out.println("결과3 : " + result3);
		
		// int length() : 문자열의 길이를 반환한다.
		String str3 = "오늘 점심은 어떤거 먹지?";
		int len = str3.length();
		System.out.println("문자열의 길이 : " + len);
		
		
		
		String name1 = new String("이순신");
		String name2 = new String("이순신");
		String name3 = "이순신";
		String name4 = "이순신";
		
		System.out.println(name1 == name2);
		System.out.println(name1.equals(name2));
		System.out.println(name3 == name4);
		System.out.println(name3.equals(name4));		

		// 1. == 주소값 비교
		// 2. equals() 내용 비교
		// equals메소드는 String객체를 생성하는 방법에 상관업이 문자열의 내용이 동일하면
		// true값을 반환. 따라서 문자열의 비교는 반드시 equals()메소드를 사용해야 한다.
	
		
		// String substring(int beginIndex): 끝까지 잘라낸 새로운 문자열을 반환
		//String substring(int beginIndex, int endIndex): ~까지 잘라낸 새로운 문자열을 반환
		String str4 = "이것이 자바다";
		String result5 = str4.substring(1);
		System.out.println("결과5: " + result5);
		
		String result6 = str4.substring(2,6);
		System.out.println("결과6: " + result6);
		
		String str5 = "이것이 자바다";
		boolean result7 = str5.contains("자바");
		System.out.println("결과: "+ result7);
		
		// boolean startsWith(String str) : 지정된 문자열로 시작하는지 여부를 반환한다.
		// boolean endsWith(String str) : 지정된 문자열로 끝나는지 여부를 반환한다.
		String str6 = "http://www.daum.net";
		String str7 = "www.google.com";
		String str8 = "www.naver.com";
		System.out.println("결과8: " + str6.startsWith("http"));
		System.out.println("결과9: " + str6.startsWith("http"));
		System.out.println("결과10: " + str6.startsWith("http"));
		System.out.println("결과11: " + str6.startsWith("com"));
		System.out.println("결과12: " + str6.startsWith("com"));
		System.out.println("결과13: " + str6.startsWith("com"));
		
		// boolean empty(): 문자열이 비어있으면 true를 반환.
		
		String str12 = "     이것이 자바다        ";
		String result17 = str12.trim();
		String str11 = "";
		System.out.println("결과17: " + "[" + result17 + "]");
			
		String str13 = "이것이 자바다. 자바 업무용 추천 도서";
		int result18 = str13.indexOf("자바");
		int result19 = str13.indexOf("파이썬");
		System.out.println("결과18 : " + result18);
		System.out.println("결과19 : " + result19);

		String str14 = "이것이 자바다";
		char result20 = str14.charAt(1);
		System.out.println("결과20: " + result20);

		String str15 = " 이순신, 김유신, 강감찬, 홍길동, 유관순";
		String[] result21 = str15.split(",");
		System.out.println("결과21: " + result21[0]);
		System.out.println("결과21: " + result21[1]);
		System.out.println("결과21: " + result21[2]);
		System.out.println("결과21: " + result21[3]);
		System.out.println("결과21: " + result21[4]);	
	}
}
package demo3;

import java.util.Scanner;

public class StringDemo2 {
public static void main(String[] args) {
	Scanner scanner = new Scanner(System.in);
	
	System.out.print("비밀번호를 입력하세요: ");
	String pwd1 = scanner.next();
	System.out.print("비밀번호를 다시 입력하세요: ");
	String pwd2 = scanner.next();
	
	System.out.println(pwd1 ==pwd2);
	System.out.println(pwd1.contentEquals(pwd2));
	
	// scanner.close();
	}
}
package demo3;

import java.util.Scanner;

public class StringDemo3 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("이메일을 입력하세요: ");
		String email = scanner.next();
		
		int endIndex = email.indexOf("@");
		String id = email.substring(0, endIndex);
		System.out.println("아이디: " + id);
		
		scanner.close();
		}
}
package demo3;

import java.util.Scanner;

public class StringDemo4 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		System.out.println("이름,국어,영어,수학 정보 입력> ");
		String text = sc.next();	// ex. 홍길동,60,80,50
		
		String[] values = text.split(",");
		System.out.println("이름: " + values[0]);
		System.out.println("국어: " + values[1]);
		System.out.println("영어: " + values[2]);
		System.out.println("수학: " + values[3]);
		
		sc.close();
	}
}
package demo3;

public class StringDemo5 {

	public static void main(String[] args) {
		
		String str = "이것이 자바다";
		System.out.println(str.length());
		
		System.out.println("이것이 자바다".length());
		
		String str2 = "       this is a java           ".trim().toUpperCase().substring(0, 5);
		System.out.println(str2);
	}
}

'자바 > api' 카테고리의 다른 글

demo4  (0) 2019.06.10
demo2  (0) 2019.06.10
demo1  (0) 2019.06.10

+ Recent posts