The original intention is to use the regular to write a Unicode string transcoding method, the first is intended to be written in conjunction with StringBuilder, But seeing Jdk7 's matcher.appendreplacement documentation for a sample code used Matcher.appendreplacement, there's already a way to replace it.
Pattern p = Pattern.compile("cat"); Matcher m = p.matcher("one cat two cats in the yard"); StringBuffer sb = new StringBuffer(); while (m.find()) { m.appendReplacement(sb, "dog"); } m.appendTail(sb); System.out.println(sb.toString());
But the impression of StringBuilder performance should be better ah, because stringbuffer with synchronized implementation, so write a simple test tested two implementations (test environment is JDK7):
@Before public void before () {for (int i = 0; i <; i++) {assertequals (excepted, unicode2s Tringwithstringbuffer (input)); Assertequals (excepted, unicode2stringwithstringbuilder (input)); }} @Test public void Testunicode2stringwithstringbuilder () {Long start = System.currenttimemillis (); for (int i = 0; i < COUNT; i++) {unicode2stringwithstringbuilder (input); } System.out.println (String.Format ("V1 StringBuilder%s takes:%s", COUNT, (System.currenttimemillis ()-start))); } @Test public void Testunicode2stringwithstringbuffer () {Long start = System.currenttimemillis (); for (int i = 0; i < COUNT; i++) {unicode2stringwithstringbuffer (input); } System.out.println (String.Format ("V2 StringBuffer%s takes:%s", COUNT, (System.currenttimemillis ()-start))); } private static final int COUNT = 10000000; Private static Final String Excepted = "Request failed with parameter error: [Action]"; Private static final String input = "\u8bf7\u6c42\u5931\u8d25\uff0c\u53c2\u6570\u9519\u8bef:[action]"; private static final Pattern Patternunicode = Pattern.compile ("\\\\u ([0-9a-za-z]{4})"); private static string Unicode2stringwithstringbuilder (final String Unicode) {if (Unicode = null) {try {Matcher Matcher = Patternunicode.matcher (Unicode); StringBuilder StringBuilder = new StringBuilder (Unicode); int offset = 0; StringBuilder replaces characters with unequal lengths to produce a positional offset while (Matcher.find ()) {String current = Matcher.group ( ); String code = matcher.group (1); String ch = string.valueof ((char) integer.parseint (Code, 16)); Stringbuilder.replace (Matcher.start () + offset, matcher.end () + offset, ch); Offset + = 1-current.length (); 1 for CH length} return Stringbuilder.tostriNg (); } catch (Exception e) {e.printstacktrace (); return Unicode; }} else {return unicode; }} private static string Unicode2stringwithstringbuffer (final String Unicode) {if (Unicode = null) { try {Matcher Matcher = Patternunicode.matcher (Unicode); StringBuffer sb = new StringBuffer (); while (Matcher.find ()) {matcher.appendreplacement (SB, string.valueof (char) integer.parseint (MATCHER.G Roup (1), 16)); } matcher.appendtail (SB); return sb.tostring (); } catch (Exception e) {e.printstacktrace (); return Unicode; }} else {return unicode; } }
The results of 100 million and 10 million executions were:
v2 StringBuffer 1000000 takes: 1815v1 StringBuilder 1000000 takes: 1364v2 StringBuffer 10000000 takes: 14107v1 StringBuilder 10000000 takes: 13316
Do not know this test section unscientific, the result is really StringBuilder faster.
Comparison of Unicode decoding methods implemented with StringBuilder and StringBuffer (Java)