String str1 = "Java";
String str2 = "Java";
String str3 = new String("Java");
package com.lec.ex1_string;
public class Ex01_String {
public static void main(String[] args) {
String str1 = "Java";
String str2 = "Java";
String str3 = new String("Java");
if(str1==str2) {
System.out.println("str1과 str2는 같은 주소를 참조");
} else {
System.out.println("str1과 str2는 다른 주소를 참조");
}
System.out.println(str1==str3? "str1과 str3은 같은 주소" : "str1과 str3은 다른 주소");
str2 = "Java~";
System.out.println(str1==str2? "수정후 같은 주소 참조":"수정후 다른 주소 참조");
System.out.println("str1 : " + str1);
System.out.println("str2 : " + str2);
}
}
str1과 str2는 같은 주소를 참조
str1과 str3은 다른 주소
수정후 다른 주소 참조
str1 : Java
str2 : Java~
\
d : (숫자와 매치, [0-9]와 동일)
\
D : (숫자가 아닌 것)
\
w : (영문자나 숫자 , [A-Za-z][0-9]와 동일
\
W : (영문자나 숫자가 아닌 문자)
. : (문자)
\
. : .
{2,} : (2번 이상 반복)
{2,4} : (2~4회 반복)
ex1. ip주소 : 198.163.2.444
([0-9]{1,3}\.){3}[0-9]{1,3}
ex2. 전화번호 : 02 777 7777 / 010 3333 3333 / 010-7777-7777 / 010.5555.5555 / 02)131.5444
\d{2,3}.?\d{3,4}.?\d{4}
ex3. 주민등록번호 : 981118-2033333 / 9812121044444
\d{2}[0-1]\d[0-3]\d-?[1-4]\d{6}
ex4. 이메일 : abc@naver.com / wj@co.kr
\w+@+\w+(\.\w+){1,2}
package com.lec.ex1_string;
import java.util.Scanner;
public class Ex05_juminNoQuiz {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("주민번호(6자리-7자리) : ");
String juminNo = scanner.next(); // 주민번호 입력 받기
int genderInt = Integer.parseInt(juminNo.substring(7,8)); 주민번호의 7번째 문자 변수 genderInt
if (genderInt == 1 || genderInt == 3) { //genderInt가 1, 3이면 남자
System.out.println("남성입니다");
} else if (genderInt == 2 || genderInt == 4) { //genderInt가 2, 4이면 여지
System.out.println("여성입니다");
} else { // else로 예외값 출력
System.out.println("유효하지 않은 주민번호입니다");
}
}
}
주민번호(6자리-7자리) : 980101-1011111
남성입니다
주민번호(6자리-7자리) : 990101-4033333
여성입니다
package com.lec.ex1_string;
import java.util.Scanner;
public class Ex06_telQuiz {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("전화번호를 입력해주세요(종료는 x)");
String tel = scanner.next();
String Odd = "";
String reverse = "";
if (tel.equalsIgnoreCase("x"))
break;
System.out.println("입력한 전화번호 : " + tel);
System.out.println("짝수번째 문자열 : ");
for (int i = 0; i < tel.length(); i++) {
if ((i % 2) == 0) {
System.out.print(tel.charAt(i));
}
}
System.out.println();
for (int i = tel.length() - 1; i >= 0; i--) {
reverse = reverse + tel.charAt(i);
}
System.out.print("문자를 거꾸로 : ");
System.out.println(reverse);
System.out.println("전화번호 맨 앞자리 : " + tel.substring(0, 3));
System.out.println("전화번호 뒷자리 : " + tel.substring(tel.lastIndexOf('-') + 1));
}
}
}
전화번호를 입력해주세요(종료는 x)
031-952-4444
입력한 전화번호 : 031-952-4444
짝수번째 문자열 :
019244
문자를 거꾸로 : 4444-259-130
전화번호 맨 앞자리 : 031
전화번호 뒷자리 : 4444
전화번호를 입력해주세요(종료는 x)
tels[i].substring(tels[i].lastIndexOf("-")+1)
전화번호 뒷자리 변수package com.lec.ex1_string;
import java.util.Scanner;
public class Ex07_searchPostTelQuiz {
public static void main(String[] args) {
String[] tels = {"010-9999-9999", "010-8888-8888", "010-7777-8888", "010-6666-8888"};
Scanner scanner = new Scanner(System.in);
while(true) {
boolean searchOk = false; //검색한 결과가 있으면 true
System.out.print("검색하고자 하는 전화번호 뒷자리는 ?(종료는 x)");
String searchTel = scanner.next();
if(searchTel.equalsIgnoreCase("x"))
break;
for(int i=0 ; i<tels.length ; i++) {
String postTel = tels[i].substring(tels[i].lastIndexOf("-")+1);
if(postTel.equals(searchTel)) {
System.out.println("검색하신 전화번호는 " + tels[i]);
searchOk = true; // 전화번호 출력여부
}//if
}//for
if(! searchOk) {
System.out.println("검색하신 전화번호 뒷자리는 없습니다");
}//if
}//while
}///main
}//class
검색하고자 하는 전화번호 뒷자리는 ?(종료는 x)6666
검색하신 전화번호 뒷자리는 없습니다
검색하고자 하는 전화번호 뒷자리는 ?(종료는 x)8888
검색하신 전화번호는 010-8888-8888
검색하신 전화번호는 010-7777-8888
검색하신 전화번호는 010-6666-8888
검색하고자 하는 전화번호 뒷자리는 ?(종료는 x)