Java 문자열 String 문자열 자르기(indexOf, substring)

별의개발자커비·2023년 2월 11일
0

Java

목록 보기
28/71
post-thumbnail

1. 문자열

  1. String 을 == 로 비교한다는 것의 의미
public class StringDemo {

	public static void main(String[] args) {
		String s1 = "Hi java!"; // hi 얘는 상수 같은 애라 어차피 같은 값이라 같은 주소에 s1과 s2를 넣어준다 = s1과 s2는 같은 주소를 갖는다
		String s2 = "Hi java!";
		s2 = "abc"; 
		

		String s3 = new String("Hi java!"); // s3, s4는 각각의 독립된 주소를 가짐
		String s4 = new String("Hi java!");
		
		System.out.println(s1);
		System.out.println(s2);
		
		System.out.println(" s1 == s2 : " +  ( s1 == s2 ) );

		System.out.println(" s3 == s4 : " +  ( s3 == s4 ) );
		
		System.out.println(" s1 == s3 : " +  ( s1 == s3 ) ); // true
		

		System.out.println(s4);

	}

}
  1. String 의 다양한 기능 (= 객체)
    : 왜냐 String은 클래스라고 볼 수 있으니까 s.000은 객체지
public class StringDemo2 {

	public static void main(String[] args) {
		String s1 = new String("Hi,");
		String s2 = new String(" Java");
		
		System.out.println("s1: " + s1.length());
		System.out.println(s1.charAt(1));
		
		s1 = s1.concat(s2);
		
		System.out.println(s1.concat(s2) + "!");
		System.out.println(s1.toLowerCase() + "!");
		System.out.println(s1.substring(4,8) + "!");
		
		String s3 = " ";
		System.out.println(s3.isEmpty());
		System.out.println(s3.isBlank());
		String s4 = "";
		System.out.println(s4.isEmpty());
		System.out.println(s4.isBlank());
		
		String s5 = "*-*";
		System.out.println(s5.repeat(10));
		
		System.out.println(s2.trim().indexOf(2));
        
        String str = "avds";
		System.out.println(str.startsWith("a"));
	}

}

s1: 3
i
Hi, Java Java!
hi, java!
Java!
false
true
true
true
---------**-
-1
true

2. 특정문자를 포함하는 문자열 자르기(indexOf, substring)

String email  = member.getEmail(); 
	String mail1  ;
	String mail2  ;
	int index = email.indexOf("@");  //  email이 포함하고있는 @ 의 인덱스 값
	mail1 = email.substring(0, index); // email의 0번째 index부터 index까지 추출
	mail2 = email.substring(index+1); // email의 index+1번째 부터 추출
	

https://magic1wp.tistory.com/9

profile
비전공자 독학러. 일단 쌔린다. 개발 공부👊

0개의 댓글