Ex36 ~ Ex37
๋ฌธ์์ด > ๋ฌธ์์ ์งํฉ
String > char[]
๋ฌธ์์ด ์ถ์ถ
- char charAt(int index)
char์ธ ์ซ์๋ฅผ ์ง์ง ์ซ์๋ก ๋ฐ๊พธ๊ธฐ ('1' > 1)
System.out.println('1' - 48);
System.out.println('9' - 48);
๋ฌธ์์ด ๊ณต๋ฐฑ ์ ๊ฑฐ
String trim()
-๋ฌธ์์ด์ ์กด์ฌํ๋ ๊ณต๋ฐฑ(Whitespace > ์คํ์ด์ค, ํญ, ๊ฐํ)์ ์ ๊ฑฐํ๋ ๋ฉ์๋
- ๋ฌธ์์ด์ ์์๊ณผ ๋์ ์๋ ๊ณต๋ฐฑ์ ์ ๊ฑฐ
String txt = " ํ๋ ๋ ์
";
System.out.printf("[%s]\n", txt);
System.out.printf("[%s]\n", txt.trim());
String s1 = "์๋ฐ";
String s2 = " ์๋ฐ ";
System.out.println(s1.equals(s2));
๋ฌธ์์ด ๊ฒ์
- boolean contains(String)
String txt = "ํ๊ธธ๋๋ ์๋
ํ์ธ์.";
//boolean contains(CharSequence s) > String ๋ฃ์ด๋ ๋จ
System.out.println(txt.contains("ํ๊ธธ๋"));
System.out.println(txt.contains("์๋ฌด๊ฐ"));
System.out.println("950215-2012457".contains("-"));
๋ฌธ์์ด ๊ฒ์
-int indexOf (์ผ์ชฝ > ์ค๋ฅธ์ชฝ ๋ฐฉํฅ์ผ๋ก ํ์)
-int lastIndexOf (์ค๋ฅธ์ชฝ > ์ผ์ชฝ)
txt = "์๋
ํ์ธ์. ํ๊ธธ๋์
๋๋ค. ๋ฐ๊ฐ์ต๋๋ค ํ๊ธธ๋์
๋๋ค. ๋ค ํ๊ธธ๋์
๋๋ค.";
index = 0;
while (true) {
index = txt.indexOf("ํ๊ธธ๋", index);
if (index == -1) {
break;
}
System.out.println(index);
index = index + 3;
}
๊ธ์ง์ด ์ฐพ๊ธฐ
.contains()
String content = "์๋
ํ์ธ์. ์ ๋ ์๋ฐ๋ฅผ ๋ฐฐ์ฐ๋ ํ์์
๋๋ค.";
String word = "๋ฐ๋ณด"; //๊ธ์ง์ด
if (content.contains(word)) {
System.out.println("๊ธ์ง์ด ๋ฐ๊ฒฌ!!");
} else {
System.out.println("๊ธ์ฐ๊ธฐ ์งํ..");
}
if (content.indexOf(word) > -1) {
System.out.println("๊ธ์ง์ด ๋ฐ๊ฒฌ!!");
} else {
System.out.println("๊ธ์ฐ๊ธฐ ์งํ..");
}
content = "์๋
ํ์ธ์. ์ ๋ ์๋ฐ๋ฅผ ๋ฐฐ์ฐ๋ ํ์์
๋๋ค.";
String[] words = { "๋ฐ๋ณด", "๋ฉ์ฒญ์ด", "๋ฉ๋กฑ" };
for (int i=0; i<words.length; i++) {
if (content.indexOf(words[i]) > -1) {
System.out.println("๊ธ์ง์ด ๋ฐ๊ฒฌ!!");
break;
}
}
System.out.println("์๋ฃ");
๋ฌธ์์ด ๋์๋ฌธ์ ๋ณ๊ฒฝ
- String toUpperCase() > ๋ฌธ์์ด์ ๋ชจ๋ ๋๋ฌธ์๋ก ๋ณํ
- String toLowerCase() > ๋ฌธ์์ด์ ๋ชจ๋ ์๋ฌธ์๋ก ๋ณํ
String content = "์ค๋ ์์
์ String Method์
๋๋ค.";
String word = "string";
if (content.toLowerCase().indexOf(word.toLowerCase()) > -1) {
System.out.println("O");
} else {
System.out.println("X");
}
System.out.println(content.toUpperCase());
System.out.println(content.toLowerCase());
System.out.println("Java".equals("java"));
System.out.println("Java".toUpperCase().equals("java".toUpperCase()));
ํจํด ๊ฒ์
- boolean startWith(String)
- boolean endsWidth(String)
String txt = "์๋ฐ ๊ฐ๋ฐ์ ๊ณผ์ ";
System.out.println(txt.startsWith("์๋ฐ"));
System.out.println(txt.startsWith("์ค๋ผํด"));
System.out.println(txt.indexOf("์๋ฐ") == 0);
๋ฌธ์์ด ์ถ์ถ
- String substring(int beginIndex, int endIndex) > end ํฌํจx
- String substring(int beginIndex)
String txt = "๊ฐ๋๋ค๋ผ๋ง๋ฐ์ฌ์์์ฐจ์นดํํํ";
System.out.println(txt.substring(3, 7)); //3~6
System.out.println(txt.substring(5, 6)); //5~5 > "๋ฐ"
System.out.println(txt.charAt(5)); // > '๋ฐ' > ๋ฌธ์์ฝ๋
//ํ์ผ ๊ฒฝ๋ก > ํ์ผ๋ช
?
//๋ง์ง๋ง ์ญ์ฌ๋์์ฐพ๊ธฐ
String path = "C:\\class\\code\\java\\JavaTest\\src\\com\\test\\java\\Ex36_String.java";
int index = path.lastIndexOf("\\");
//System.out.println(index);
String filename = path.substring(index + 1);
System.out.println(filename); //ํ์ผ๋ช
์ถ์ถ
//Ex36_String.java
//ํ์ฅ์ ์๋ ํ์ผ๋ช
์ถ์ถ
index = filename.lastIndexOf(".");
String filenameWithoutExtension = filename.substring(0, index);
System.out.println(filenameWithoutExtension);
//ํ์ฅ์ ์ถ์ถ
String extension = filename.substring(index);
System.out.println(extension);
๋ฌธ์์ด ์นํ(๋ฐ๊พธ๊ธฐ) & ๋ง์คํน
- String replace(String old, String new)
String txt = "์๋
ํ์ธ์. ํ๊ธธ๋์
๋๋ค.";
//txt = txt.replace("ํ๊ธธ๋", "์๋ฌด๊ฐ"); >>์๋ณธ ์์
System.out.println(txt.replace("ํ๊ธธ๋", "์๋ฌด๊ฐ"));
System.out.println(txt);
String content = "๊ฒ์ํ์ ๊ธ์ ์์ฑํฉ๋๋ค. ๋ฐ๋ณด์ผ!!";
String word = "๋ฐ๋ณด";
//๊ธ์ง์ด ์๋ณด์ด๊ฒ ์ฒ๋ฆฌ > Masking
System.out.println(content.replace(word, "**"));
//replace์์ ""(๋น๋ฌธ์์ด)๋ก ๋ฐ๊พธ๊ธฐ > ์ํ๋ ๋ฌธ์์ด์ ์ญ์ ํ๊ธฐ!!!!!
System.out.println(content.replace(word, ""));
๋ฌธ์์ด ๋ถ๋ฆฌ
- String[] split(String delimiter)
- ๊ตฌ๋ถ์๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ฌธ์์ด ์๋ฅด๋ ๋ฉ์๋
String name = ",ํ๊ธธ๋,,์๋ฌด๊ฐ,ํํํ,";
String[] list = name.split(","); //๊ตฌ๋ถ์๋ ์ฌ๋ผ์ง๋ค
for (int i=0; i<list.length; i++) {
System.out.println(list[i] + "," + list[i].length());
}
๋ฌธ์์ด ๋น๊ต
String s1 = "Hong";
String s2 = "hong";
System.out.println(s1.compareTo(s2));
System.out.println(s1.compareToIgnoreCase(s2));
String์ char[]๋ก
String s5 = "ํ๊ธธ๋";
char[] clist = s5.toCharArray();
System.out.println(Arrays.toString(clist));
1. ๋ฉ๋ชจ๋ฆฌ์ ๊ณต๊ฐ์ ํ๋ฒ ํ ๋น๋๋ฉด ๋ ์ด์ ๋๋ฆฌ๊ฑฐ๋ ์ค์ผ ์ ์๋ค.
2. "๋ฌธ์์ด์ ๋ถ๋ณ(Immutable)์ด๋ค." > ํ๋ฒ ๋ง๋ค์ด์ง ๋ฌธ์์ด์ ์ ๋ ์์ ํ ์ ์๋ค.
3. ๋ฌธ์์ด์ ์ฐธ์กฐํ์ด๋ค.
๋ฌธ์์ด > ํ๋ฉด ์๋๋ ํ๋(๋ฉ๋ชจ๋ฆฌ ๋ง์ด ์์)
1. ํฐ ๋ฌธ์์ด ์กฐ์
2. ์ฆ์ ๋ฌธ์์ด ์กฐ์
StringBuilder
-ํจ์จ์ ์ผ๋ก ๋ฌธ์์ด ์กฐ์ ๊ฐ๋ฅ
StringBuilder name2 = new StringBuilder("ํ๊ธธ๋");
String๊ณผ StringBuilder ์ฒ๋ฆฌ ์๋ ๋น๊ต
long begin = System.currentTimeMillis();
long begin = System.nanoTime();
String txt1 = "ํ๊ธธ๋";
for (int i=0; i<100000; i++) {
txt1 = txt1 + "."; //์ฆ์ ์์
}
long end = System.nanoTime();
System.out.println(txt1.length()); //100003
System.out.println(end - begin + "ns"); //6.1s
begin = System.nanoTime();
StringBuilder txt2 = new StringBuilder("ํ๊ธธ๋");
for (int i=0; i<100000; i++) {
txt2.append("."); //txt2 = txt2 + "."
}
end = System.nanoTime();
System.out.println(txt2.length()); //100003
System.out.println(end - begin + "ns");
// 100003
// 5.206526700ns > String
//
// 100003
// 0.004093900ns > StringBuilder
'ํ๋ก๊ทธ๋๋ฐ ๊ณต๋ถ > Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[13์ผ์ฐจ] ๋๋ฏธ ๋ฐ์ดํฐ(๊ฐ์ ๋ฐ์ดํฐ) ๋ง๋ค๊ธฐ (1) | 2023.02.21 |
---|---|
[12์ผ์ฐจ] ํด๋์ค(Class) (0) | 2023.02.21 |
[10์ผ์ฐจ] ๋ค์ฐจ์ ๋ฐฐ์ด(Array) (0) | 2023.02.15 |
[10์ผ์ฐจ] ๋ฐฐ์ด Array (2) (0) | 2023.02.15 |
[9์ผ์ฐจ] ๋ฐฐ์ด(Array), ์ ๋ ฌ (0) | 2023.02.13 |