ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • String methods - replaceAll, substring, charAt, toUpperCase
    Java 2021. 4. 6. 20:57

    replaceAll(String regex, String replacement)

    • 주어진 정규식과 일치하는 문자열을 replacement문자열로 대체합니다.
    String a = "맑고 깨끗한 유기농 녹차";
    a = a.replaceAll("녹차", "커피");
    System.out.println(a);
    // 결과값 : 맑고 깨끗한 유기농 커피
    
    String s = "_-.391dafa21*&@";
    s = s.replaceAll("[^a-z0-9-_.]", "");
    System.out.println(s);
    // [^a-z0-9-_.] : 영어 소문자, 한자리수 숫자, 특수문자(-_.)를 제외한 모든 문자를 삭제합니다.
    // ^ : 조건을 제외한 다른 문자. 
    // 결과값 : _-.391dafa21
    

     

    substring(int beginIndex, int endIndex)

    • 해당 인덱스 범위에 포함된 문자열을 return합니다.
    • 인덱스 0 부터 시작합니다.
    • endIndex의 이전 인덱스 까지 포함합니다.
    String b = "123456789";
    String subStr = b.substring(0, 5);
    System.out.println(subStr);
    // 결과값 : 12345

     

    charAt(int index)

    • 해당 인덱스에 있는 char값을 return합니다.
    String c = "123456789";
    char ch1 = c.charAt(0);
    System.out.println(ch1);
    // 결과값 : 1
    
    char lastChar = c.charAt(c.length() - 1);
    System.out.println(lastChar);
    // 결과값: 9

     

    toUpperCase() / toLowerCase() 

    • 이 문자열의 모든 문자를 대문자 혹은 소문자로로 변환합니다.
    String d = "abcdEFGH";
    d = d.toLowerCase();
    System.out.println(d);
    // 결과값: abcdefgh
    
    d = d.toUpperCase();
    System.out.println(d);
    // 결과값: ABCDEFGH

     

    java.lang.String

CokeWorld DevLog