카테고리 없음
getMiddle
므시칸곰틔군
2018. 4. 30. 17:57
/**가운데 문자 가져오기 홀수면 1개 짝수면 가운대 2개*/
String getMiddle(String word) {
int count = word.getBytes().length;
int half_count = count / 2;
LogUtil.e("" + half_count);
String str = null;
if (count >= 3) {
if (count % 2 == 0) {
LogUtil.w("나누어서 나머지를 가져보니 짝수이다");
str = word.substring(half_count - 1, half_count + 1);
} else {
LogUtil.w("나누어서 나머지를 가져보니 홀수이다");
str = word.substring(half_count, half_count + 1);
}
} else {
if (half_count == 0) {
// LogUtil.w(half_count + " , 나눈값0 나누어 보니 홀수이다");
str = word.substring(half_count);
} else {
// LogUtil.w(half_count + " ,,나눈값1 나누어 보니 짝수이다");
str = word.substring(half_count - 1, half_count + 1);
}
}
return str;
}