![[JAVA] 39. JRE 라이브러리](https://image.inblog.dev?url=https%3A%2F%2Finblog.ai%2Fapi%2Fog%3Ftitle%3D%255BJAVA%255D%252039.%2520JRE%2520%25EB%259D%25BC%25EC%259D%25B4%25EB%25B8%258C%25EB%259F%25AC%25EB%25A6%25AC%26logoUrl%3Dhttps%253A%252F%252Finblog.ai%252Finblog_logo.png%26blogTitle%3Dsxias&w=2048&q=75)
String 라이브러리
package ex08;
// String 함수 다 사용해보기
public class Str01 {
public static void main(String[] args) {
// 1. 문자열의 길이 : String.length() (길이 제한)
String s1 = "abcd";
System.out.println("s1's length : " + s1.length());
// 2. 특정 index의 문자 확인하기 : String.charAt()
String s2 = "abcd";
System.out.println("s2's char at index 2 : " + s2.charAt(2));
// 3. 문자열 비교 : String.equals()
System.out.println("abcd".equals("abcd"));
// 4. 문자열 추출 : String.substring()
// Check : First Index == 0 && End Index == n or n - 1?
String s3 = "abcd";
System.out.println(s3.substring(1, 3));
// 5. 문자열 검색 : String.indexOf()
// Various version == Method Overloading
String s4 = "abcd";
System.out.println(s4.indexOf('c'));
// 6. 문자열 포함 여부 : String.contains()
String s5 = "abcd";
System.out.println(s5.contains("k"));
// 7. 문자열 대소문자 변경 : String.toUpperCase() / String.toLowerCase()
String s6 = "Abcd";
System.out.println(s6.toUpperCase());
System.out.println(s6.toLowerCase());
// 8. 문자열 치환 : String.replace()
int age = 10;
String s7 = "내 나이는 $age고 난 내 나이 $age가 좋아".replace("$age", age + "");
System.out.println(s7);
// 9. 앞 뒤 공백 제거 : String.trim()
String s8 = "abcd ";
System.out.println(s8.trim());
// 10. 문자열 분리 : String.split()
// parsing 문자가 n개 있을 때, 분리하면 n+1개의 문자열 배열 생성
String s9 = "ab:cd";
String[] r9 = s9.split(":");
System.out.println(r9[0]);
System.out.println(r9[1]);
}
}
활용 문제
- 전화번호의 지역번호를 이용하여 부산에 사는 사람의 수를 구하는 프로그램을 작성하시오.
문제 분석
- 부산의 지역번호 : 051
- 전화번호 분리 : split()
- String에서 지역번호만 추출 : substring()
- 괄호 앞까지만 자르려면? : indexOf()
- 부산의 지역번호 “051”과 맞는지 비교 : equals() or contains()
- 맞을 때만 count++
코드 작성
package ex08;
public class Str02 {
public static void main(String[] args) {
String nums = """
031)533-2112,
02)223-2234,
02)293-4444,
051)398-3434,
02)498-3434,
051)398-3434,
043)3222-3434
""";
// 부산에 사는 고객은 몇명인가요?
//System.out.println("부산에 사는 고객은 2명입니다");
// 1. split(,)로 String[]로 옮기기
String[] num = nums.split(",");
int[] index = new int[num.length];
int count = 0;
for (int i = 0; i < num.length; i++) {
// 2. indexOf(")") 위치 찾기
index[i] = num[i].indexOf(')');
// 3. substring으로 지역번호 추출하기
num[i] = num[i].substring(0, index[i]);
// 3-1. 공백 제거
num[i] = num[i].trim();
// System.out.println(num[i]);
// 4. 051 지역번호를 equals로 확인 or contains로 확인하여 고객수 구하기
if (num[i].equals("051")) count++;
// if (num[i].contains("051")) count++;
// System.out.println(count);
}
// 결과 출력 : replace
System.out.println("부산에 사는 사람은 $count명입니다.".replace("$count", count + ""));
}
}
결과

내용 정리
- split()을 사용하여 문자열 배열을 나눌 수 있음
- substring()을 통해 필요한 문자열만 추출 가능
- 공백이 들어감에 주의 : trim() - 공백 제거
- 문자열 포함 여부 확인 : equals() or contains()
- contains()의 경우 갯수에 상관없이 들어가 있기만 하면 true를 반환
- replace()를 사용하여 문자열 내의 변수를 특수문자 등과 함께 사용하여 변환 가능
Share article