题目
找到被指的新类型字符
java代码
package com.lizhouwei.chapter5;
/**
* @Description: 找到被指的新类型字符
* @Author: lizhouwei
* @CreateDate: 2018/4/25 21:20
* @Modify by:
* @ModifyDate:
*/
public class Chapter5_19 {
public String pointNewChar(String str, int k) {
if (str == null || k < 0 || k > str.length()) {
return null;
}
char[] chars = str.toCharArray();
int count = 0;
for (int i = k - 1; i > -1; i--) {
if (Character.isLowerCase(chars[i])) {
break;
}
count++;
}
if ((count & 1) == 1) {
return str.substring(k - 1, k + 1);
}
if (Character.isLowerCase(chars[k])) {
return String.valueOf(chars[k]);
} else {
return str.substring(k, k + 2);
}
}
//测试
public static void main(String[] args) {
Chapter5_19 chapter = new Chapter5_19();
String str = "aaABCDEcBCg";
String result = chapter.pointNewChar(str, 7);
System.out.println("aaABCDEcBCg 第7位的新字符为:" + result);
}
}
结果