正如标题所述,这是一个替换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
查看更多关于java字符串中${}或者{}等的占位符替换工具类的详细内容...