-
[15] StringDevelpment/Java Sample Source 2020. 9. 13. 21:29
String
123456789101112131415161718192021222324252627282930313233343536373839404142package ex_sting;import java.util.Scanner;public class String01{public static void main(String[] args){// String은 두가지 특징을 가지고 있다.// 1) 객체 생성 방법이 두가지다.(명시적, 암시적)// 2) 한번 생성된 문자열은 변하지 않는다.(불변적 특징)// 암시적 객체생성String s1 = "abc";String s2 = "abc";if(s1 == s2) // 객체 간의 비교시 주소값 비교가 된다.System.out.println("주소가 같습니다.");elseSystem.out.println("주소가 다릅니다.");// 명시적 객체생성String s3 = new String("abc");System.out.println();if(s1 == s3)System.out.println("주소가 같습니다.");elseSystem.out.println("주소가 다릅니다.");Scanner sc = new Scanner(System.in);System.out.print("입력 : ");String s4 = sc.next();if( s4.equalsIgnoreCase("a") )System.out.println("a");elseSystem.out.println("a x");}}cs 실행결과
주소가 같습니다.
주소가 다릅니다.
입력 : s
a x
String 2
123456789101112131415161718192021222324252627282930313233343536373839404142434445package ex_sting;public class String_Method02{public static void main(String[] args){String name = "Hong Gil Dong";System.out.println("문자열 name의 길이 : " + name.length());int index = name.indexOf('g');System.out.println("맨처음 문자 g의 위치 : " + index);index = name.indexOf("Gil");System.out.println("맨처음 문장 Gil의 위치 : " + index);index = name.lastIndexOf('g');System.out.println("마지막 문자 g의 위치 : " + index);char ch = name.charAt(5);System.out.println("추출한 문자 : " + ch);String ss = name.substring(0, 5);System.out.println("추출한 문장 : " + ss);String s2 = ss.trim(); // 앞뒤 공백 제거!!if(s2.equals(" Hong ".trim()))System.out.println("값이 같습니다.");String[] arr = name.split(" ");for(int i = 0; i < arr.length; i++)System.out.println(arr[i]);String number = "90";int nNum = Integer.parseInt(number);System.out.println(nNum + 10);}}cs 실행결과
문자열 name의 길이 : 14
맨처음 문자 g의 위치 : 3
맨처음 문장 Gil의 위치 : 5
마지막 문자 g의 위치 : 13
추출한 문자 : G
추출한 문장 : Hong
값이 같습니다.
Hong
Gil
Dong
100
String 3
123456789101112131415161718192021222324252627282930package ex_sting;import java.util.Scanner;public class String03{public static void main(String[] args){// 키보드에서 아무값이나 무작위로 입력!!// 입력 받은 문자열에서 소문자 a가 몇개 있는지 판별하는 로직 구현!!/* 입력 : safafdsdfsdf dfsdf dfsf* a의 갯수 : ?*/String strMsg = "";int nCount = 0;Scanner sc = new Scanner(System.in);System.out.print("입력 : ");strMsg = sc.nextLine();for(int i = 0; i < strMsg.length(); i++)if(strMsg.charAt(i) == 'a')nCount++;System.out.println("a의 갯수 : " + nCount);}}cs 실행결과
입력 : asdfasdcsdssdfsdfdsaa
a의 갯수 : 4
String 4
1234567891011121314151617181920212223242526272829303132333435package ex_sting;import java.util.Scanner;public class String04{public static void main(String[] args){// 이메일 입력, @앞의 문자가 6보다 작거나, 10보다 클경우// 입력한 이메일이 올바르지 않습니다./* 이메일 : asd@n.com* 이메일 형식이 올바르지 않습니다.** 이메일 : asdasd@k.com* asdasd - 이메일 확인 성공*/String strEmail = "";String strTemp = "";Scanner sc = new Scanner(System.in);System.out.print("이메일 : ");strEmail = sc.nextLine();strTemp = strEmail.substring(0, strEmail.indexOf('@'));if(strTemp.length() < 6 || strTemp.length() > 10)System.out.println("이메일 형식이 올바르지 않습니다.");elseSystem.out.printf("%s - 이메일 확인 성공\n", strTemp);}}cs 실행결과
이메일 : awwqwe@a.net
awwqwe - 이메일 확인 성공
String 5
123456789101112131415161718192021222324252627282930313233package ex_sting;import java.util.Scanner;public class String05{public static void main(String[] args){// 회문수 : 앞뒤로 읽어도 같은 숫자. ex ) 1221, 2772// 키보드에서 3자리 이상의 숫자 입력// 해당 숫자가 회문수 인지 확인하는 로직을 구현. (String)int i = 0;int j = 0;String strNumber = "";Scanner sc = new Scanner(System.in);strNumber = sc.nextLine();for(i = 0, j = strNumber.length()-1; i < strNumber.length()/2; i++, j--){if(strNumber.charAt(i) != strNumber.charAt(j))break;}if(i == strNumber.length()/2)System.out.println("회문수");elseSystem.out.println("회문수 아님");}}cs 실행결과
12321
회문수
String 6
12345678910111213141516171819202122232425262728293031323334353637383940package ex_sting;import java.util.Scanner;public class String06{public static void main(String[] args){/* 검색 : 김수현* [김수현]* 도둑들* 해품달** 검색 : 김수혁* 해당 배우가 존재하지 않습니다.*/String[][] actor = {{"[송강호]", "박쥐", "관상"},{"[김수현]", "도둑들", "해품달"},{"[전지현]", "엽그", "암살"}};Scanner sc = new Scanner(System.in);System.out.print("검색 : ");String strMsg = sc.nextLine();boolean bSearch = false;out : for(int i = 0; i < actor.length; i++)if(strMsg.equals(actor[i][0].substring(actor[i][0].indexOf('[')+1, actor[i][0].indexOf(']')))){bSearch = true;for(String str : actor[i])System.out.println(str);break out;}if(bSearch == false)System.out.println("해당 배우가 존재하지 않습니다.");}}cs 실행결과
검색 : 김수현
[김수현]
도둑들
해품달
'Develpment > Java Sample Source' 카테고리의 다른 글
[17] Generic (0) 2020.09.13 [16] Class (0) 2020.09.13 [14] arrary-multi (0) 2020.09.13 [13] Array-single (0) 2020.09.13 [12] random, scanner (0) 2020.09.13