String str = '123456'; char[] arr = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'}; // 定义字母数组 String result = ''; String firstTwo = str.substring(0, 2); // 截取字符串前两位 int num = Integer.parseInt(firstTwo); // 将前两位转换为整数 if(num >= 10 && num <= 19) { // 如果前两位是10-19 result += arr[num-10]; // 将对应的字母加入结果字符串中 } else { // 如果前两位不是10-19 result += firstTwo; // 将前两位直接加入结果字符串中 } result += str.substring(2); // 将剩余的字符串加入结果字符串中 System.out.println(result); // 输出结果字符串

Java代码:将字符串前两位数字替换为字母

原文地址: https://www.cveoy.top/t/topic/noc2 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录