import java.util.Scanner;
public class StringEx {
public static void main(String[] args) {
String str1 = " TheJoEun Academy ";
String str2 = " theJoEun Academy ";
System.out.println( str1.charAt(2) ); // index ์ ์๋ ๋ฌธ์ ์ถ์ถ
System.out.println( str1.concat(str2) ); // ๋ฌธ์์ด ์ฐ๊ฒฐ
System.out.println( str1.contains("Academy") ); // ๋ฌธ์์ด ํฌํจ ์ฌ๋ถ (t/f)
System.out.println( str1.equals(str2) ); // ๋ฌธ์์ด ์ผ์น ์ฌ๋ถ (t/f)
System.out.println( str1.equalsIgnoreCase(str2) ); // ๋์๋ฌธ์ ๊ตฌ๋ถ ์์ด ์ผ์น ์ฌ๋ถ (t/f)
System.out.println( str1.indexOf("A") ); // ํด๋น ๋ฌธ์์ ์ฒซ index ๋ฐํ, ์์ผ๋ฉด -1
System.out.println( str1.lastIndexOf("e") ); // ํด๋น ๋ฌธ์์ ๋ง์ง๋ง index ๋ฐํ, ์์ผ๋ฉด -1
System.out.println( str1.trim() ); // ๋ฌธ์์ด ์์ชฝ ๊ณต๋ฐฑ ์ ๊ฑฐ
System.out.println( str1.length() ); // ๋ฌธ์์ด ๊ธธ์ด (๊ธ์์)[๊ณต๋ฐฑํฌํจ]
System.out.println( str1.substring(10) ); // index ์์ ๋ฌธ์์ด์ ์๋ฅด๊ณ , ๋ค์ ๋ฌธ์์ด ๋ฐํ
System.out.println( str1.substring(10, 17) ); // index~(index2-1) ๊น์ง ๋ฐํ
// index 10๋ฒ์งธ ๋ถํฐ 16๋ฒ์งธ ๊น์ง์ ๋ฌธ์์ด์ ๊ฐ์ ธ์จ๋ค.
// Academy
// [10]~[16]
// split("๊ตฌ๋ถ์") : (๊ตฌ๋ถ์)๋ฅผ ๊ธฐ๋ถ์ผ๋ก ๋ฌธ์์ด์ ์๋ผ์ ๋ฐฐ์ด๋ก ๋ฐํ
String[] sp = str1.split(""); // ""(๋น ๋ฌธ์์ด)์ ๊ธฐ๋ถ์ผ๋ก ํ๋ฉด, ํ ๊ธ์์ฉ ๋ฐฐ์ด์์๋ก ๋ฐํ
for (int i = 0; i < sp.length; i++) {
System.out.println(i + " : \t " + sp[i]);
}
System.out.println();
for (int i = 0; i < str1.length(); i++) {
char ch = str1.charAt(i);
System.out.print(ch + " ");
}
System.out.println();
// String.split( ์ ๊ทํํ์ )
// : ์ ๊ทํํ์์์ + ๊ธฐํธ๋ ์ฐ์ ์ฐ์ฐ์๊ฐ ์๋ ๋ค๋ฅธ ์๋ฏธ๋ก ์ฌ์ฉ
// ์ฐ์ ์ฐ์ฐ์ + ๋ก ๊ตฌ๋ถํ๋ ค๋ฉด ์์ \\ ๊ธฐํธ๋ฅผ ๋ถ์ฌ์ฃผ์ด์ผํ๋ค.
Scanner sc = new Scanner(System.in);
// String cal = "10+20";
String cal = sc.nextLine();
String[] num = cal.split("\\+");
if( cal.contains("+") ) {
int a = Integer.parseInt( num[0] );
int b = Integer.parseInt( num[1] );
int result = a + b;
System.out.println(result);
}
System.out.println("ํผ์ฐ์ฐ์1 : " + num[0]);
System.out.println("ํผ์ฐ์ฐ์2 : " + num[1]);
sc.close();
}
}
Java
๋ณต์ฌ