首页 > 技术文章 > 标红字符串里面第一个数字

fans4-334 2016-10-19 14:21 原文

 1 /**
 2          * 标红一个字符串里面的第一个数字
 3          * 
 4          * @param str
 5          * @return
 6          */
 7         private static String changeNumberColor(String str) {
 8             String array[] = str.split("");
 9             String newStr = "";
10             for (int i = 0; i < array.length; i++) {
11                 if (i + 2 < array.length) {
12                     if (isOctNumber(array[i]) && !isOctNumber(array[i + 1]) && !isOctNumber(array[i + 2])) {
13                         String s = array[i];
14                         String colorNumber = "<font color='#FF0000'>" + s + "</font>";
15                         newStr = str.replace(String.valueOf(s), colorNumber);
16                         break;
17                     } else if (isOctNumber(array[i]) && isOctNumber(array[i + 1]) && !isOctNumber(array[i + 2])) {
18                         String s = array[i] + array[i + 1];
19                         String colorNumber = "<font color='#FF0000'>" + s + "</font>";
20                         newStr = str.replace(String.valueOf(s), colorNumber);
21                         break;
22                     } else if (isOctNumber(array[i]) && isOctNumber(array[i + 1]) && isOctNumber(array[i + 2])) {
23                         String s = array[i] + array[i + 1] + array[i + 2];
24                         String colorNumber = "<font color='#FF0000'>" + s + "</font>";
25                         newStr = str.replace(String.valueOf(s), colorNumber);
26                         break;
27                     }
28 
29                 }
30 
31             }
32 
33             return newStr;
34         }
35 
36         // 判断一个字符串是否是十进制数字
37         private static boolean isOctNumber(String s) {
38             boolean flag = false;
39             if ("0".equals(s) || "1".equals(s) || "2".equals(s) || "3".equals(s) || "4".equals(s) || "5".equals(s)
40                     || "6".equals(s) || "7".equals(s) || "8".equals(s) || "9".equals(s)) {
41                 flag = true;
42             }
43 
44             return flag;
45         }

 

推荐阅读