String 객체 사용법

niraaah·2023년 4월 24일
1

혼자하는 스터디

목록 보기
16/25
public class StringEx {
	public static void main(String[] args) {
		String a = new String(" C#");
		String b = new String(",c++ ");
		
        // the length of String
		System.out.println("the length of " + a + " is " + a.length());
        
        // is there "String" in the String?
		System.out.println(a.contains("#"));
		
		a = a.concat(b);	// connect String
		System.out.println(a);
		
		// remove space at the front and end of String
		a = a.trim();		
		System.out.println(a);
		
		a = a.replace("C#","Java");	// replace A into B
		System.out.println(a);
		
		String s[] = a.split(",");	// split String
		for (int i = 0; i < s.length; i++)
			System.out.println("split string" + i + ": " + s[i]);
		
		// return index n to the end as a String
		a = a.substring(5);		
		System.out.println(a);
		
		char c = a.charAt(2);	// return index m-th char
		System.out.println(c);
		
	}
}

>>결과:
the length of  C# is 3
true
 C#,c++ 
C#,c++
Java,c++
split string0: Java
split string1: c++
c++
+
profile
코딩천재

0개의 댓글