본문 바로가기

프로그래밍/알고리즘(자바)

가운데 글자 가져오기 Level 1 #substring #length(),length,size() 차이

가운데 글자 가져오기 Level 1

https://programmers.co.kr/learn/challenge_codes/82


length와 length(), size()들의 차이점은 아래 링크에 잘 설명 되어 있다.


length와 length(), size()들의 차이점


즉 length는 배열, length()는 문자열, size는 콜렉션, 셋 객체에서 사용한다.


class StringExercise{
    String getMiddle(String word){
        return word.substring((word.length()-1)/2,word.length()/2+1) ; 
        //length()-1 후에 나누기 2 해야 함에 유의!!
        //subString이 아니라 substring이다.
        //substring(여기부터, 이 곳 까지!)
    }                                                                 

    // 아래는 테스트로 출력해 보기 위한 코드입니다.
    public static void  main(String[] args){
        StringExercise se = new StringExercise();
        System.out.println(se.getMiddle("power"));
    }
}