正如標題所述,這是一個替換java字符串中${}或者{}等占位符的工具類,其處理性能比較令人滿意。該類主要通過簡單的改寫myatis框架中的generictokenparser類得到。在日常開發過程中,可以將該類進行簡單的改進或封裝,就可以用在需要打印日志的場景中,現在張貼出來給有需要的人,使用方式參考main方法,不再贅述!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
public class parser { /** * 將字符串text中由opentoken和closetoken組成的占位符依次替換為args數組中的值 * @param opentoken * @param closetoken * @param text * @param args * @return */ public static string parse(string opentoken, string closetoken, string text, object... args) { if (args == null || args.length <= 0 ) { return text; } int argsindex = 0 ; if (text == null || text.isempty()) { return "" ; } char [] src = text.tochararray(); int offset = 0 ; // search open token int start = text.indexof(opentoken, offset); if (start == - 1 ) { return text; } final stringbuilder builder = new stringbuilder(); stringbuilder expression = null ; while (start > - 1 ) { if (start > 0 && src[start - 1 ] == '\\' ) { // this open token is escaped. remove the backslash and continue. builder.append(src, offset, start - offset - 1 ).append(opentoken); offset = start + opentoken.length(); } else { // found open token. let's search close token. if (expression == null ) { expression = new stringbuilder(); } else { expression.setlength( 0 ); } builder.append(src, offset, start - offset); offset = start + opentoken.length(); int end = text.indexof(closetoken, offset); while (end > - 1 ) { if (end > offset && src[end - 1 ] == '\\' ) { // this close token is escaped. remove the backslash and continue. expression.append(src, offset, end - offset - 1 ).append(closetoken); offset = end + closetoken.length(); end = text.indexof(closetoken, offset); } else { expression.append(src, offset, end - offset); offset = end + closetoken.length(); break ; } } if (end == - 1 ) { // close token was not found. builder.append(src, start, src.length - start); offset = src.length; } else { ///////////////////////////////////////僅僅修改了該else分支下的個別行代碼//////////////////////// string value = (argsindex <= args.length - 1 ) ? (args[argsindex] == null ? "" : args[argsindex].tostring()) : expression.tostring(); builder.append(value); offset = end + closetoken.length(); argsindex++; //////////////////////////////////////////////////////////////////////////////////////////////// } } start = text.indexof(opentoken, offset); } if (offset < src.length) { builder.append(src, offset, src.length - offset); } return builder.tostring(); } public static string parse0(string text, object... args) { return parser.parse( "${" , "}" , text, args); } public static string parse1(string text, object... args) { return parser.parse( "{" , "}" , text, args); } /** * 使用示例 * @param args */ public static void main(string... args) { //{}被轉義,不會被替換 system.out.println(parser.parse( "{" , "}" , "我的名字是\\{},結果是{},可信度是%{}" , "雷鋒" , true , 100 )); system.out.println(parser.parse0( "我的名字是${},結果是${},可信度是%${}" , "雷鋒" , true , 100 )); system.out.println(parser.parse1( "我的名字是{},結果是{},可信度是%{}" , "雷鋒" , true , 100 )); // 輸出結果如下: // 我的名字是{},結果是true,可信度是%100 // 我的名字是雷鋒,結果是true,可信度是%100 // 我的名字是雷鋒,結果是true,可信度是%100 } } |
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。如果你想了解更多相關內容請查看下面相關鏈接
原文鏈接:https://blog.csdn.net/nmgrd/article/details/77387191