더 자세하게 Java

[Java][Ch3] String 클래스_for each

써치킴 2022. 5. 13. 02:24

String 클래스 메서드

  • 숫자 -> 문자 
    • Integer.toString(int a) : 정수형 a가 문자열로 변환됨
    • String.valueOf(int a) : 정수형 a가 문자열로 변환됨
  • 문자 -> 숫자
    • Interger.parseInt(String s) : 문자열 s가 정수형으로 변환됨
      • ※ 주의 : "a"와 같은 문자열은 숫자로 변경 불가 -> "13" 와 같은 문자가 숫자로 변경 가능
  • 문자 추출
    • String변수.charAt(int a) : a번째 방에 문자가 무엇인가?
  • 문자열찾기
    • String변수.indexOf("S") : S가 몇번방에 있는가?
    •   .indexOf( "찾을 특정 문자" , "시작할 위치" )
      • str.indexOf("all", 0)
    • String변수.lastIndexOf("S") : 뒤에서 제일 먼저 있는 문자열 S의 방을 찾아줌
      • 방번호는 고정이다 앞에서 0부터 ~ 
  • 문자 길이
    • String변수.length() : 방의 개수
  • 공백 제거
    • String trim = String변수.trim()
  • 문자 교체
    • replace(String s1, String s2) : s1을 s2로 바꿔라
  • 문자열 추출
    • String변수.substring(8) : 8번째 문자부터 끝까지 출력하라
    • String변수.substring(4,10) : 4번방부터 10번방까지 출력
    • split(구분자)
      • String names = "강호동,유재석#이경규%";
      • String[] allname = names.split(,|#|%);  → OR 연산자 사용하여 구분자로 문자열 추출

MainEx.java

public class MainEx {
     public static void main(String[] args) {
          String str = "abc";
          
//        char data[] = {'a','b','c'};
//        String str2 = new String(data);
//        System.out.println(str);
//        System.out.println(str2);
          
          // 숫자 -> 문자 : toString(), valueOf()
          int a = 10;
          double b = 12.3;
          
          // Integer 클래스 소속
          String s1 = Integer.toString(a);      // a가 문자열로 변환됨
          System.out.println(s1+123);
          // String 클래스 소속
          String s2 = String.valueOf(a);
          System.out.println(s2+123);
          
          // 문자 -> 숫자 : parseInt()
          // Integer 클래스 소속
          String s3 = "34";
          int c = Integer.parseInt(s3);
          System.out.println(c+123);
          
          // "a"와 같은 문자는 숫자로 변경 불가 => error 발생
//        String s4 = "a4";
//        int d = Integer.parseInt(s4);
//        System.out.println(d+123);
          
          String s5 = "Hello World";
          // charAt(int a) : a번방의 문자가 무엇이냐
          char cv1 = s5.charAt(0);
          System.out.println("cv1 : " +cv1);
          char cv2 = s5.charAt(3);
          System.out.println("cv2 : " +cv2);
          char cv3 = s5.charAt(6);
          System.out.println("cv3 : " +cv3);
          char cv4 = s5.charAt(10);
          System.out.println("cv4 : " +cv4);
          // 방번호 초과 => error 발생
//        char cv5 = s5.charAt(11);
//        System.out.println("cv5 : " +cv5);
          
     /*   //    (String s) : 문자열 s가 몇번방에 있느냐
          String s6 = "Hello World";
          int index1 =s6.indexOf("H");
          System.out.println("H : " + index1);
          // 제일 먼저 있는 문자열(l)의 방을 찾아줌
          int index2 =s6.indexOf("l");
          System.out.println("l : " + index2);
          int index3 =s6.indexOf("r");
          System.out.println("r : " + index3);
          int index4 =s6.indexOf(" ");
          System.out.println("  : " + index4);
          // 처음시작하는 문자열(lo의 l)의 방을 찾아줌
          int index5 =s6.indexOf("lo");
          System.out.println("lo : " + index5);
          // 요청한 문자열을 갖고있지 않으면 -1이 출력됨
          int index6 =s6.indexOf("a");
          System.out.println("a : " + index6);*/
          
          System.out.println();
          // lastIndexOf(String s) : 뒤에서부터 s가 몇번방에 있느냐 (방번호는 고정 0부터~)
          String s6 = "Hello World";
          int index1 =s6.lastIndexOf("H");
          System.out.println("H : " + index1);
          // 뒤에서부터 제일 먼저 있는 문자열(l)의 방을 찾아줌
          int index2 =s6.lastIndexOf("l");
          System.out.println("l : " + index2);
          int index3 =s6.lastIndexOf("r");
          System.out.println("r : " + index3);
          int index4 =s6.lastIndexOf(" ");
          System.out.println("  : " + index4);
          // 뒤에서부터 문자열(lo의 l)의 방을 찾아줌
          int index5 =s6.lastIndexOf("lo");
          System.out.println("lo : " + index5);
          // 요청한 문자열을 갖고있지 않으면 -1이 출력됨
          int index6 =s6.lastIndexOf("a");
          System.out.println("a : " + index6);
          
          System.out.println();
          // length() : 길이 출력(방의 개수)
          String s7 = "Hello a World";
          System.out.println("s7.length() : " + s7.length());
          
          // trim() : 공백제거
          String s8 = "    Hello a World    ";
          System.out.println("s8.length() : " + s8.length());
          String trim = s8.trim();
          System.out.println("trim.length() : " + trim.length());
          
          System.out.println();
          // replace(char a, char b) : a문자를 b문자로 바꿔라
          String s9 = "Hello a World";
          String s10 = s9.replace("Hello", "bye");
          System.out.println("s9 : " + s9);
          System.out.println("s10 : " + s10);
          String s11 = s9.replace("He", "b");
          System.out.println("s11 : " + s11);
          String s12 = s9.replace("l", "p");
          System.out.println("s12 : " + s12);
          
          System.out.println();
          // ★중요★
          // substring(int beginindex) : beginindex부터 끝까지 출력 (beginindex 전 문자열은 자름)
          String s13 = s9.substring(8);
          System.out.println("s13 : " + s13);
          
          // ★중요★
          // substring(int beginindex, int endindex) : beginindex부터 endindex 전방까지 출력
          // endindex방은 출력되지 않음
          String s14 = s9.substring(4, 10);
          System.out.println("s14 : "+ s14);
          
          System.out.println();
          // split("String s") : s형 기준으로 잘라서 String 배열로 리턴해준다.
          /*String names = "김제동,강호동,유재석,이경규,김현준";
          System.out.println(names.length());
          String[] allName = names.split(",");
          for (int i = 0; i < allName.length; i++) {
              System.out.println(allName[i]);
          }*/
          
//        String names = "김제동 강호동 유재석 이경규 김현준";
          String names = "김제동,강호동,유재석#이경규%김현준";
          System.out.println(names.length());
          String[] allName = names.split(" |,|#|%");      // & 연산자 사용
          for (int i = 0; i < allName.length; i++) {
              System.out.println(allName[i]);
          }
          // foreach문은 출력용이다!
          System.out.println();
          for(String s : allName){
              System.out.println(s);
          }
     }
}

Foreach