在Java中执行大量字符串替换的最快方法 [英] Fastest way to perform a lot of strings replace in Java

查看:2667
本文介绍了在Java中执行大量字符串替换的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须编写某种解析器来获取String并用其他字符替换某些字符集。代码如下所示:

I have to write some sort of parser that get a String and replace certain sets of character with others. The code looks like this:

noHTMLString = noHTMLString.replaceAll("</p>", "\n");
noHTMLString = noHTMLString.replaceAll("<br/>", "\n\n");
noHTMLString = noHTMLString.replaceAll("<br />", "\n\n");
//here goes A LOT of lines like these ones

这个函数非常长执行大量的字符串替换。这里的问题是它需要花费很多时间,因为它被调用的方法很多次,降低了应用程序的性能。

The function is very long and performs a lot of strings replaces. The issue here is that it takes a lot of time because the method it's called a lot of times, slowing down the application performance.

我在这里阅读了一些关于使用的线程StringBuilder作为替代方案但它没有ReplaceAll方法,因为它在这里被注意到 string.replaceAll()性能受到字符串不变性的影响? String类中的replaceAll方法与

I have read some threads here about using StringBuilder as an alternative but it lacks the ReplaceAll method and as it's noted here Does string.replaceAll() performance suffer from string immutability? the replaceAll method in String class works with


匹配模式& Matcher和Matcher.replaceAll()使用StringBuilder来存储最终返回的值
,所以我不知道切换到StringBuilder是否会真正减少执行替换的时间。

Match Pattern & Matcher and Matcher.replaceAll() uses a StringBuilder to store the eventually returned value so I don't know if switching to StringBuilder will really reduce the time to perform the substitutions.

您是否知道以快速方式快速完成大量字符串替换?你对这个问题有什么建议吗?

Do you know a fast way to do a lot of String replace in a fast way? Do you have any advice for this problem?

谢谢。

编辑:我必须创建一个包含html文本字段的报告。对于每一行,我正在调用替换这些字符串中的所有html标记和特殊字符的方法。使用完整报告,解析所有文本需要3分钟以上。问题是我必须经常调用该方法

EDIT: I have to create a report that have a few fields with html text. For each row I'm calling the method that replaces all the html tags and special characters inside these strings. With a full report it takes more than 3 minutes to parse all the text. The problem is that I have to invoke the method very often

推荐答案

我发现org.apache.commons.lang.StringUtils是如果你不想打扰StringBuffer,最快。

I found that org.apache.commons.lang.StringUtils is the fastest if you don't want to bother with the StringBuffer.

你可以像这样使用它:

noHTMLString = StringUtils.replace(noHTMLString,< / p>,\ n);

You can use it like this:
noHTMLString = StringUtils.replace(noHTMLString, "</p>", "\n");

我做了性能测试比我自定义的StrinBuffer解决方案更糟糕,类似于@extraneon提出的解决方案。

I did performance testing it was fester than my custom StrinBuffer solution similar to the one @extraneon proposed.

这篇关于在Java中执行大量字符串替换的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆