-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringEx.java
More file actions
47 lines (34 loc) · 1.25 KB
/
Copy pathStringEx.java
File metadata and controls
47 lines (34 loc) · 1.25 KB
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
39
40
41
42
43
44
45
46
47
package com.javaex.api.stringclass;
public class StringEx {
public static void main(String[] args) {
String s1 = "Java"; // 리터럴
String s2 = new String("Java"); // 메모리에 새로 만들기
String s3 = "Java";
System.out.println(s1 == s2);
System.out.println(s1 == s3);
// String 생성자
char[] letters = {'J', 'a', 'v', 'a'};
String s4 = new String(letters);
// valueOf 메서드
String s5 = String.valueOf(3.14159f);
// 유용한 메서드
String str = "Java Programming is Fun?";
System.out.println(str.charAt(5)); // 5번 인덱스의 char
System.out.println(str.indexOf("Fun"));
System.out.println(str.indexOf("fun"));
// replace
System.out.println(str.replace('?', '!'));
System.out.println(str.replaceAll("Fun", "Funny"));
System.out.println(str); // 위에서 문자열 바꿨지만 원본은 안 바뀜
String s6 = " Hello "; // 화이트 스페이스
String s7 = ", Java";
s6 = s6.trim(); // Whitespace 날리기
System.out.println(s6 + s7);
// 문자열 분리 : split();
String[] split = str.split(" ");
System.out.println(split);
for (String data: split) {
System.out.println(data);
}
}
}