프로그래밍 언어/JAVA

코테에서 잘 쓰이는 내장함수

Mev01 2021. 9. 2. 14:51

String


substring: 인덱스에 해당하는 부분 문자열

startsWith, endsWith: 문자열이 특정 문자열로 시작하는지/끝나는지 검사

replaceAll: 정규표현식에 해당하는 문자를 바꿈

split: 정규표현식에 해당하는 문자로 문자열을 나눔

toUpperCase, toLowerCase: 문자열을 대/소문자로 변경

trim: 문자열 앞뒤 공백 제거

matches: 정규표현식과 일치할 때 true 반환

concat: 문자 결합

format: 특정 서식으로 변환된 문자열을 반환

String str = "ABcdef ghi ";

System.out.println(str.substring(2, 5)); // 인덱스 2~4, "cde"
System.out.println(str.startsWith("AB")); // true
System.out.println(str.replaceAll("[g]", "Z")); // g라는 문자 Z로, "ABcdef Zhi "
System.out.println(Arrays.toString(str.split("[ ]"))); // 공백으로 구분, [ABcdef, ghi]
System.out.println(str.toLowerCase()); // "abcdef ghi "
System.out.println(str.trim()); // "ABcdef ghi"
System.out.println(str.matches("[A-Za-z]")); // A-Z, a-z까지의 문자만 있어야 함. 공백이 있어서 false
System.out.println(str.concat("aaa")); // "ABcdef ghi aaa"
System.out.println(str + "aaa"); // concat과 같음

int num = 1234567;
System.out.println(String.format("%,d", num)); // "1,234,567"
System.out.println(String.format("%13s", str)); // 최소 13글자로 유지, 부족하면 공백을 앞에 추가, "  ABcdef ghi "
System.out.println(String.format("%-13s", str)); // 부족하면 공백을 뒤에 추가, "ABcdef ghi   "

 

 

Collections


sort: 정렬

Collections.sort(subList, new Comparator(){
     @Override
     public int compare(Object o1, Object o2){
      	return map.get(o2) - map.get(o1);
     }
});

 

 

'프로그래밍 언어 > JAVA' 카테고리의 다른 글

JAVA 정규식  (0) 2021.08.26
빠른 입출력  (0) 2021.01.30