Back-End/Java

[Java] 가운데 문자 추출하기

미피뿌 2018. 11. 11. 15:20
반응형

가운데 문자 추출하기


가운데 문자 추출 

=> 짝수면 가운데 두자리 모두 추출

class Solution {
    
  public String solution(String word) {
      int length = word.length();
      int index = length / 2;
      return (length%2 == 0) ? word.substring(index-1, index+1) :
          word.substring(index, index+1);
  }
  
  public static void main(String[] args) {
        Solution s = new Solution();
        System.out.println(s.solution("hello"));
  }
}

 

실행결과 

l

반응형