๐ ๊ณต๋ถํ๋ ์ง์ง์ํ์นด๋ ์ฒ์์ด์ง?
[JAVA ์ฝ๋ฉ ์ด๋ณด] String(์คํธ๋ง) Method ๋ณธ๋ฌธ
728x90
๋ฐ์ํ
<๋ณธ ๋ธ๋ก๊ทธ๋ codechacha ๋์ ๋ธ๋ก๊ทธ๋ฅผ ์ฐธ๊ณ ํด์ ๊ณต๋ถํ๋ฉฐ ์์ฑํ์์ต๋๋ค :-)>
๐ String Method
๐ ๊ธธ์ด ๋ฐํ & ๋ฌธ์์ด ์ฒดํฌ & ๋ฌธ์ ์ฐพ๊ธฐ
import java.util.*;
import java.io.*;
// tip: each public class is put in its own file
public class main
{
// tip: arguments are passed via the field below this editor
public static void main(String[] args)
{
String str = "My name is gani";
// ๊ธธ์ด ๋ฐํ
System.out.println(str.length());
// ๋น ๋ฌธ์์ด ์ฒดํฌ
System.out.println(str.isEmpty());
// ๋ฌธ์ ์ฐพ๊ธฐ
System.out.println(str.charAt(0)); // 'M' -> ๋ฌธ์ ๋ฐํ
System.out.println(str.indexOf("a")); // 0 -> ์ธ๋ฑ์ค ๋ฐํ
System.out.println(str.lastIndexOf("i")); // i -> ๋ง์ง๋ง์ผ๋ก ๋ฌธ์๊ฐ ์ํ ์ธ๋ฑ์ค ๋ฐํ
}
}
๐ ๋ฌธ์ ์๋ฅด๊ธฐ & ๋ฌธ์ ์นํ
import java.util.*;
import java.io.*;
// tip: each public class is put in its own file
public class main {
// tip: arguments are passed via the field below this editor
public static void main(String[] args) {
String str = "My name is gani";
// ๋ฌธ์ ์๋ฅด๊ธฐ
System.out.println(str.substring(1, 3)); // ์ธ๋ฑ์ค 1 ์ด์ 3 ๋ฏธ๋ง ์์น์ ๋ฌธ์์ด ๋ฐํ
System.out.println(str.substring(3)); // ์ธ๋ฑ์ค 3 ๋ฏธ๋ง ์์น์ ๋ฌธ์์ด ๋ฐํ
// ๋ฌธ์ ์นํ(๋ฐ๊พธ๊ธฐ)
// replace([๊ธฐ์กด๋ฌธ์], [๋ฐ๊ฟ๋ฌธ์])
System.out.println(str.replace('g', 'n')); // ๋ชจ๋ [๊ธฐ์กด ๋ฌธ์]๋ฅผ [๋ฐ๊ฟ ๋ฌธ์]๋ก ์นํ
// replaceAll([์ ๊ท์], [๋ฐ๊ฟ๋ฌธ์])
System.out.println(str.replaceAll(".", "/")); // "/////" -> ์ ๊ท์์ ๋ง์ถฐ ๋ฌธ์ ์นํ (์ ๊ท์ "." ์ ๋ชจ๋ ๋ฌธ์๋ฅผ ์๋ฏธ)
String str2 = "gani";
// replaceFirst([๊ธฐ์กด๋ฌธ์], [๋ฐ๊ฟ๋ฌธ์])
System.out.println(str2.replaceFirst('i', 'p')); // ์ฌ๋ฌ ๋ฌธ์ ์ค ์ฒซ๋ฒ์งธ๋ง ์นํ
}
}
๐ ๋ฌธ์ ๋์ผ ์ฌ๋ถ ํ๋จ & ๋ฌธ์ ๋น๊ต & ๋ฌธ์ ํฌํจ ์ฌ๋ถ ํ๋จ & ๋ฌธ์์ ์ซ์ ์นํ
import java.util.*;
import java.io.*;
// tip: each public class is put in its own file
public class main {
// tip: arguments are passed via the field below this editor
public static void main(String[] args) {
String str = "apple";
// ๋ฌธ์ ๋์ผ ์ฌ๋ถ ํ๋จ
// ์๋ฐ string์ ๊ฒฝ์ฐ, ํด๋์ค๋ก์จ Call by Referenceํํ๋ก ์์ฑ ์ ์ฃผ์๊ฐ์ด ๋ถ์ฌ
// => Stringํ์
์ ์ ์ธํ์๋๋ ๊ฐ์ ๊ฐ์ ๋ถ์ฌํ๋๋ผ๋ ์๋ก๊ฐ์ ์ฃผ์๊ฐ์ด ๋ค๋ฅด๋ค.
// => ๊ฐ ๋น๊ต๋ก๋ equals๋ฅผ ์ฌ์ฉ
System.out.println(str.equals("apple"));
//๋ฌธ์ ๋น๊ต
/**
* str๊ณผ ๊ฐ์ผ๋ฉด 0
* str์ด ์ฌ์ ์์ผ๋ก ์์ด๋ฉด -1
* str์ด ์ฌ์ ์์ผ๋ก ๋ค๋ฉด 1
* str์ด ๋ง์ง๋ง ๋ฌธ์๋ง ๋ค๋ฅด๋ฉด, ๋ง์ง๋ง ๋ฌธ์์ ์ฌ์ ์ ์ฐจ์ด ๋ฐํ
*/
System.out.println(str.compareTo("applq"));
//๋ฌธ์ ํฌํจ ์ฌ๋ถ ํ๋จ
System.out.println(str.contains("app"));
//๋ฌธ์ <-> ์ซ์ ๋ณํ
System.out.println(Integer.parseInt("100")); // ๋ฌธ์์ด "100"์ ์ซ์ 100์ผ๋ก ๋ณํ
System.out.println(Integer.toString(100)); // ์ซ์ 100์ ๋ฌธ์์ด "100"์ผ๋ก ๋ณํ
}
}
๐ ๋ฌธ์์ด ๋ถ๋ฆฌ & ๋ฌธ์ ์๋ค ๊ณต๋ฐฑ ์ ๊ฑฐ
import java.util.*;
import java.io.*;
// tip: each public class is put in its own file
public class main {
// tip: arguments are passed via the field below this editor
public static void main(String[] args) {
String str = "apple";
// ๋ฌธ์์ด ๋ถ๋ฆฌ
System.out.println(str.split(" ")); // ๊ณต๋ฐฑ์ผ๋ก ๊ตฌ๋ถ๋ ๋ฌธ์์ด str์ ๋ถ๋ฆฌํ์ฌ String[] ๋ฐฐ์ด๋ก ๋ฐํ
System.out.println(str.split()); // ๋์ด์ฐ๊ธฐ ์๋ ๋ฌธ์์ด str์ ํ ๋ฌธ์์ฉ ๋ถ๋ฆฌํ์ฌ String[] ๋ฐฐ์ด๋ก ๋ฐํ
// ๋ฌธ์ ์๋ค ๊ณต๋ฐฑ ์ ๊ฑฐ
System.out.println(str.trim()); // str์ ์๋ค ๊ณต๋ฐฑ์ ์ ๊ฑฐํ๋ค. ๋ฌธ์์ด ์ฌ์ด์ ๊ณต๋ฐฑ์ ์ ๊ฑฐํ์ง ์๋๋ค.
}
}
728x90
๋ฐ์ํ
'๐ฆฅ ์ฝํ > JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JAVA ์ฝ๋ฉ ์ด๋ณด] Queue (ํ) Method (0) | 2023.07.16 |
---|---|
[JAVA ์ฝ๋ฉ ์ด๋ณด] Stack (๋ฆฌ์คํธ) Method (0) | 2023.07.16 |
[JAVA ์ฝ๋ฉ ์ด๋ณด] List (๋ฆฌ์คํธ) Method (0) | 2023.07.16 |
[JAVA ์ฝ๋ฉ ์ด๋ณด] StringBuilder(์คํธ๋ง๋น๋) Method (0) | 2023.07.16 |
[JAVA ์ฝ๋ฉ ์ด๋ณด] ๋ฐฐ์ด ์ ๋ ฌ (์ค๋ฆ์ฐจ์, ๋ด๋ฆผ์ฐจ์) (0) | 2023.07.16 |
Comments