package k20231206;

import java.util.Arrays;
import java.util.Scanner;
public class StringMethodTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = " tjoeunit ";
// length() : 문자열을 구성하는 문자의 개수를 얻어온다
System.out.println("문자열을 구성하는 문자의 개수: " + str.length());
System.out.println(str);
// trim() : 문자열 앞, 뒤의 불필요한 빈 칸(공백)을 제거한다.
System.out.println(str.trim());
System.out.println("불필요한 빈 칸을 제거한 문자의 개수: " + str.trim().length());
// System.out.print("문자열 입력: ");
// 데이터 입력 단계에서 불필요한 공백을 제거하고 입력받으려면 아래와 같이 trim() 메소드를 사용한다.
// String name = scanner.nextLine().trim();
// System.out.println(name.length());
str = "0304223185600";
// charAt() : 문자열에서 지정된 index(0부터 시작) 위치의 문자를 얻어온다
System.out.println("성별: " + str.charAt(6));
// 키보드로 1문자만 입력 받기
// char ch = scanner.nextLine().charAt(0);
// System.out.println(ch);
str = "TjoeunIT";
// toUpprtCase(): 영문자를 무조건 대문자로 변환
// toLowerCase(): 영문자를 무조건 소문자로 변환
System.out.println("무조건 대문자로: " + str.toUpperCase());
System.out.println("무조건 소문자로: " + str.toLowerCase());
str = "8304221185600";
// substring(a): 문자열의 a번째 index의 문자부터 문자열의 끝까지 얻어온다.
// substring(a, b): 문자열의 a번째 index의 문자부터 b-1 문자열까지 얻어온다.
System.out.println("주민등록번호 뒷자리: " + str.substring(6));
System.out.println("생년: " + str.substring(0,2));
System.out.println("월: " + str.substring(2,4));
System.out.println("일: " + str.substring(4,6));
// repeat(): 문자열을 인수로 지정한 개수만큼 반복한다.
System.out.println(str.substring(0,7) + "******");
System.out.println(str.substring(0,7) + "*".repeat(6));
str = "ittjoeunit";
// indexOf(): 문자열 왼쪽부터 검색해서 인수로 지정한 문자열이 최초로 나타나는 index를 얻어온다.
// lastIndexOf(): 문자열 오른쪽부터 검색해서 인수로 지정한 문자열이 최초로 나타나는 index를 얻어온다.
System.out.println(str.indexOf("it"));
System.out.println(str.lastIndexOf("it"));
// indexOf(), lastIndexOf() 메소드로 인수로 지정한 문자열이 없으면 -1을 리턴한다.
System.out.println(str.indexOf("It"));
System.out.println(str.lastIndexOf("IT"));
// indexOf(), lastIndexOf() 메소드로 실행 결과가 0이상이면 지정된 문자열이 포함되어 있다는 의미이다.
// contains(): 문자열에 인수로 지정한 문자열이 포함되어 있으면 true, 없으면 false를 리턴한다.
System.out.println(str.contains("it"));
System.out.println(str.contains("IT"));
str = "abc 123 가나다";
// split(): 문자열을 인수로 지정한 구분자를 경계로 나눠서 배열로 리턴한다.
System.out.println(str.split(" "));
System.out.println(str.split(" ").length);
System.out.println(Arrays.toString(str.split(" ")));
System.out.println(str.split(" ")[0]);
str = "ittjoeunit";
// replace(a, b) : 문자 열의 모든 a를 b로 치환한다.
System.out.println(str.replace("i","아이"));
System.out.println("==============================================");
// "-"과 "-"사이의 모든 문자열을 "*"로 변경해서 출력한다.
// 하이픈 사이를 배열로 나누면 0인덱스 123 / 1인덱스 45678 / 2인덱스 12345
// 0인덱스의 문자열 수를 구하고 3 5 5
// 이때, "-"과 "-"사이의 모든 문자열의 개수는 가변적이다.
str = "123-45678-12345";
}
}
'java&eclipse 코딩 알고리즘 > 20231206' 카테고리의 다른 글
| WeekdayTest (0) | 2023.12.14 |
|---|---|
| StringTest (0) | 2023.12.14 |
| SosooTest (0) | 2023.12.14 |
| SooinsooTest (0) | 2023.12.14 |
| ClassIncludeTest (0) | 2023.12.14 |