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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
| package Lecture9;
public class StringMain03 {
public static void main(String[] args) {
String str = new String("Hello");
String str2 = new String("Hello");
// String str = "Hello";
// String str2 = "Hello";
System.out.println("str : [" + str + "]");
System.out.println("str : [" + str2 + "]");
/**
* 주소비교 => 따라서 str = new String("Hello");와 str2 = new String("Hello");는 다름
*/
if(str==str2) {
System.out.println("str == str2");
} else {
System.out.println("str != str2");
}
// 문자열 비교
// equals
boolean bool = str.equals(str2);
if(bool) {
System.out.println("equals() : str == str2");
} else {
System.out.println("equals() : str != str2");
}
// str.equals(str2); str과 str2에 들어있는 문자열이 서로 같니?
// "Hello".equals(str);
// equalsIgnoreCase
boolean bool2 = str.equalsIgnoreCase(str2);
if(bool2) {
System.out.println("equalsIgnoreCase() : str == str2");
} else {
System.out.println("equalsIgnoreCase() : str != str2");
}
// stratsWith
str = "Hello";
str2 = "Hello World";
String sub = "Hello";
bool = str.startsWith(sub);
if(bool) {
System.out.println("[" + str + "]는 [" + sub + "]문자열로 시작합니다.");
} else {
System.out.println("[" + str + "]는 [" + sub + "]문자열로 시작하지 않습니다.");
}
// endsWith
str = "hello txt";
bool = str.endsWith("txt");
if(bool) {
System.out.println("[" + str + "]는 텍스트파일입니다.");
} else {
System.out.println("[" + str + "]는 텍스트파일이 아닙니다.");
}
//int compareTo(String str)
str = "boat";
str2 = "board";
int compare = str.compareTo(str2);
if(compare == 0) {
System.out.println(str + " == " + str2);
} else if(compare < 0) {
System.out.println(str + " < " + str2);
} else {//compare > 0
System.out.println(str + " > " + str2);
}
// 예제
String[] names = {"강길동", "홍길동", "홍길순", "김길동", "윤길동", "박홍철", "홍길동"};
System.out.println("< 이름이 홍길동인 사람 검색 >");
for(int i = 0; i < names.length; i++) {
if(names[i].equals("홍길동"))
System.out.println(names[i]);
}
System.out.println("< 성이 홍씨인 사람 검색 >");
for(String name : names) {
if(name.startsWith("홍")) {
System.out.println(name);
}
}
System.out.println("< 이름이 길동인 사람 검색 >");
for(String name : names) {
if(name.endsWith("길동")) {
System.out.println(name);
}
}
System.out.println("< 홍이 포함된 사람 검색 >");
for(String name : names) {
if(name.contains("홍")) {
System.out.println(name);
}
}
str = "Hello World";
System.out.println("문자열 : " + str);
int searchIdx = str.indexOf("o");
System.out.println("\"o\" indexOf() 위치 : " + searchIdx);
searchIdx = str.indexOf("p");
System.out.println("\"p\" indexOf() 위치 : " + searchIdx); // 없으면 -1로 나옴
searchIdx = str.indexOf("o", 5);
System.out.println("\"o\" indexOf(5) 위치 : " + searchIdx);
searchIdx = str.lastIndexOf("o");
System.out.println("\"o\" lastIndexOf() 위치 : " + searchIdx);
str = "Hello World";
char ch = 'l';
System.out.println("<" + str + "에서 " + ch + " 위치 >");
searchIdx = str.indexOf(ch);
while(searchIdx != -1) {
System.out.println("검색된 위치 : " + searchIdx);
searchIdx = str.indexOf(ch, searchIdx+1);
}
System.out.println("====================================");
searchIdx = -1;
while((searchIdx = str.indexOf(ch, searchIdx+1)) != -1) {
System.out.println("검색된 위치 : " + searchIdx);
}
System.out.println("====================================");
searchIdx = str.length();
while((searchIdx = str.lastIndexOf(ch, searchIdx-1)) != -1) {
System.out.println("검색된 위치 : " + searchIdx);
}
}
}
|