Java一次替换字符串中的多个不同子字符串(或以最有效的方式)
Posted
技术标签:
【中文标题】Java一次替换字符串中的多个不同子字符串(或以最有效的方式)【英文标题】:Java Replacing multiple different substring in a string at once (or in the most efficient way) 【发布时间】:2010-11-22 12:54:46 【问题描述】:我需要以最有效的方式替换字符串中的许多不同子字符串。 除了使用 string.replace 替换每个字段的蛮力方式之外,还有其他方法吗?
【问题讨论】:
【参考方案1】:使用replaceAll() 方法怎么样?
【讨论】:
可以在正则表达式中处理许多不同的子字符串 (/substring1|substring2|.../)。这完全取决于 OP 试图进行什么样的替换。 OP 正在寻找比str.replaceAll(search1, replace1).replaceAll(search2, replace2).replaceAll(search3, replace3).replaceAll(search4, replace4)
更高效的东西【参考方案2】:
如果您要多次更改字符串,那么使用 StringBuilder 通常更有效(但要测量您的性能以找出答案):
String str = "The rain in Spain falls mainly on the plain";
StringBuilder sb = new StringBuilder(str);
// do your replacing in sb - although you'll find this trickier than simply using String
String newStr = sb.toString();
每次对字符串进行替换时,都会创建一个新的字符串对象,因为字符串是不可变的。 StringBuilder 是可变的,也就是说,它可以随心所欲地改变。
【讨论】:
恐怕没用。每当替换的长度与原始长度不同时,您就需要进行一些移位,这可能比重新构建字符串更昂贵。还是我错过了什么?【参考方案3】:StringBuilder
将更有效地执行替换,因为它的字符数组缓冲区可以指定为所需的长度。StringBuilder
的设计不仅仅是追加!
当然,真正的问题是这是否是优化过头了? JVM 非常擅长处理多个对象的创建和随后的垃圾回收,并且像所有优化问题一样,我的第一个问题是您是否已经测量过并确定这是一个问题。
【讨论】:
【参考方案4】:如果您正在操作的字符串很长,或者您正在操作许多字符串,那么使用 java.util.regex.Matcher 可能是值得的(这需要预先编译时间,所以它不会'如果您的输入非常小或您的搜索模式经常更改,则效率不高)。
以下是一个完整示例,基于从地图中获取的标记列表。 (使用来自 Apache Commons Lang 的 StringUtils)。
Map<String,String> tokens = new HashMap<String,String>();
tokens.put("cat", "Garfield");
tokens.put("beverage", "coffee");
String template = "%cat% really needs some %beverage%.";
// Create pattern of the format "%(cat|beverage)%"
String patternString = "%(" + StringUtils.join(tokens.keySet(), "|") + ")%";
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(template);
StringBuffer sb = new StringBuffer();
while(matcher.find())
matcher.appendReplacement(sb, tokens.get(matcher.group(1)));
matcher.appendTail(sb);
System.out.println(sb.toString());
编译正则表达式后,扫描输入字符串通常很快(尽管如果您的正则表达式很复杂或涉及回溯,那么您仍然需要进行基准测试以确认这一点!)
【讨论】:
是的,不过需要对迭代次数进行基准测试。 我认为你应该在做"%(" + StringUtils.join(tokens.keySet(), "|") + ")%";
之前转义每个标记中的特殊字符@
请注意,使用 StringBuilder 可以提高速度。 StringBuilder 不同步。 edit whoops 仅适用于 java 9
未来读者:对于正则表达式,“(”和“)”将包含要搜索的组。 “%”在文本中算作文字。如果您的条款不以“%”开头和结尾,则将找不到它们。所以调整两个部分的前缀和后缀(文本+代码)。【参考方案5】:
检查一下:
String.format(str,STR[])
例如:
String.format( "Put your %s where your %s is", "money", "mouth" );
【讨论】:
【参考方案6】:Rythm 是一个 java 模板引擎,现在发布了一个名为 String interpolation mode 的新功能,它允许您执行以下操作:
String result = Rythm.render("@name is inviting you", "Diana");
上面的例子表明你可以按位置将参数传递给模板。 Rythm 还允许您按名称传递参数:
Map<String, Object> args = new HashMap<String, Object>();
args.put("title", "Mr.");
args.put("name", "John");
String result = Rythm.render("Hello @title @name", args);
注意 Rythm 非常快,大约比 String.format 和速度快 2 到 3 倍,因为它将模板编译成 java 字节码,运行时性能非常接近与 StringBuilder 的串联。
链接:
检查full featured demonstration 阅读a brief introduction to Rythm 下载latest package或 fork it【讨论】:
这是非常古老的功能,可用于许多模板语言,例如速度,甚至 JSP。它也没有回答不需要搜索字符串采用任何预定义格式的问题。 有趣,接受的答案提供了一个示例:"%cat% really needs some %beverage%.";
,那%
分隔标记不是预定义格式吗?你的第一点就更搞笑了,JDK提供了很多“老功能”,有些是从90年代开始的,为什么人们还要费心使用它们呢?您的 cmets 和 downvoting 没有任何实际意义
当已有很多模板引擎,并且广泛使用如 Velocity 或 Freemarker 启动时,引入 Rythm 模板引擎有什么意义?当核心 Java 功能绰绰有余时,为什么还要引入另一个产品。我真的怀疑您对性能的陈述,因为 Pattern 也可以编译。希望看到一些真实的数字。
格林,你没抓住重点。提问者想要替换任意字符串,而您的解决方案将仅替换预定义格式的字符串,例如 @ 前缀。是的,该示例使用 % 但仅作为示例,而不是限制因素。所以你的回答并没有回答问题,因此是负面的。【参考方案7】:
public String replace(String input, Map<String, String> pairs)
// Reverse lexic-order of keys is good enough for most cases,
// as it puts longer words before their prefixes ("tool" before "too").
// However, there are corner cases, which this algorithm doesn't handle
// no matter what order of keys you choose, eg. it fails to match "edit"
// before "bed" in "..bedit.." because "bed" appears first in the input,
// but "edit" may be the desired longer match. Depends which you prefer.
final Map<String, String> sorted =
new TreeMap<String, String>(Collections.reverseOrder());
sorted.putAll(pairs);
final String[] keys = sorted.keySet().toArray(new String[sorted.size()]);
final String[] vals = sorted.values().toArray(new String[sorted.size()]);
final int lo = 0, hi = input.length();
final StringBuilder result = new StringBuilder();
int s = lo;
for (int i = s; i < hi; i++)
for (int p = 0; p < keys.length; p++)
if (input.regionMatches(i, keys[p], 0, keys[p].length()))
/* TODO: check for "edit", if this is "bed" in "..bedit.." case,
* i.e. look ahead for all prioritized/longer keys starting within
* the current match region; iff found, then ignore match ("bed")
* and continue search (find "edit" later), else handle match. */
// if (better-match-overlaps-right-ahead)
// continue;
result.append(input, s, i).append(vals[p]);
i += keys[p].length();
s = i--;
if (s == lo) // no matches? no changes!
return input;
return result.append(input, s, hi).toString();
【讨论】:
【参考方案8】:以下内容基于Todd Owen's answer。该解决方案的问题是,如果替换包含在正则表达式中具有特殊含义的字符,您可能会得到意想不到的结果。我还希望能够选择性地进行不区分大小写的搜索。这是我想出的:
/**
* Performs simultaneous search/replace of multiple strings. Case Sensitive!
*/
public String replaceMultiple(String target, Map<String, String> replacements)
return replaceMultiple(target, replacements, true);
/**
* Performs simultaneous search/replace of multiple strings.
*
* @param target string to perform replacements on.
* @param replacements map where key represents value to search for, and value represents replacem
* @param caseSensitive whether or not the search is case-sensitive.
* @return replaced string
*/
public String replaceMultiple(String target, Map<String, String> replacements, boolean caseSensitive)
if(target == null || "".equals(target) || replacements == null || replacements.size() == 0)
return target;
//if we are doing case-insensitive replacements, we need to make the map case-insensitive--make a new map with all-lower-case keys
if(!caseSensitive)
Map<String, String> altReplacements = new HashMap<String, String>(replacements.size());
for(String key : replacements.keySet())
altReplacements.put(key.toLowerCase(), replacements.get(key));
replacements = altReplacements;
StringBuilder patternString = new StringBuilder();
if(!caseSensitive)
patternString.append("(?i)");
patternString.append('(');
boolean first = true;
for(String key : replacements.keySet())
if(first)
first = false;
else
patternString.append('|');
patternString.append(Pattern.quote(key));
patternString.append(')');
Pattern pattern = Pattern.compile(patternString.toString());
Matcher matcher = pattern.matcher(target);
StringBuffer res = new StringBuffer();
while(matcher.find())
String match = matcher.group(1);
if(!caseSensitive)
match = match.toLowerCase();
matcher.appendReplacement(res, replacements.get(match));
matcher.appendTail(res);
return res.toString();
这是我的单元测试用例:
@Test
public void replaceMultipleTest()
assertNull(ExtStringUtils.replaceMultiple(null, null));
assertNull(ExtStringUtils.replaceMultiple(null, Collections.<String, String>emptyMap()));
assertEquals("", ExtStringUtils.replaceMultiple("", null));
assertEquals("", ExtStringUtils.replaceMultiple("", Collections.<String, String>emptyMap()));
assertEquals("folks, we are not sane anymore. with me, i promise you, we will burn in flames", ExtStringUtils.replaceMultiple("folks, we are not winning anymore. with me, i promise you, we will win big league", makeMap("win big league", "burn in flames", "winning", "sane")));
assertEquals("bcaacbbcaacb", ExtStringUtils.replaceMultiple("abccbaabccba", makeMap("a", "b", "b", "c", "c", "a")));
assertEquals("bcaCBAbcCCBb", ExtStringUtils.replaceMultiple("abcCBAabCCBa", makeMap("a", "b", "b", "c", "c", "a")));
assertEquals("bcaacbbcaacb", ExtStringUtils.replaceMultiple("abcCBAabCCBa", makeMap("a", "b", "b", "c", "c", "a"), false));
assertEquals("c colon backslash temp backslash star dot star ", ExtStringUtils.replaceMultiple("c:\\temp\\*.*", makeMap(".", " dot ", ":", " colon ", "\\", " backslash ", "*", " star "), false));
private Map<String, String> makeMap(String ... vals)
Map<String, String> map = new HashMap<String, String>(vals.length / 2);
for(int i = 1; i < vals.length; i+= 2)
map.put(vals[i-1], vals[i]);
return map;
【讨论】:
【参考方案9】:算法
替换匹配字符串(不使用正则表达式)的最有效方法之一是将Aho-Corasick algorithm 与高性能Trie(发音为“try”)、快速hashing 算法和高效collections 实现结合使用.
简单代码
一个简单的解决方案利用 Apache 的 StringUtils.replaceEach
,如下所示:
private String testStringUtils(
final String text, final Map<String, String> definitions )
final String[] keys = keys( definitions );
final String[] values = values( definitions );
return StringUtils.replaceEach( text, keys, values );
这会降低大文本的速度。
快速编码
Aho-Corasick 算法的Bor's implementation 引入了更多复杂性,通过使用具有相同方法签名的外观成为实现细节:
private String testBorAhoCorasick(
final String text, final Map<String, String> definitions )
// Create a buffer sufficiently large that re-allocations are minimized.
final StringBuilder sb = new StringBuilder( text.length() << 1 );
final TrieBuilder builder = Trie.builder();
builder.onlyWholeWords();
builder.removeOverlaps();
final String[] keys = keys( definitions );
for( final String key : keys )
builder.addKeyword( key );
final Trie trie = builder.build();
final Collection<Emit> emits = trie.parseText( text );
int prevIndex = 0;
for( final Emit emit : emits )
final int matchIndex = emit.getStart();
sb.append( text.substring( prevIndex, matchIndex ) );
sb.append( definitions.get( emit.getKeyword() ) );
prevIndex = emit.getEnd() + 1;
// Add the remainder of the string (contains no more matches).
sb.append( text.substring( prevIndex ) );
return sb.toString();
基准测试
对于基准测试,缓冲区是使用randomNumeric 创建的,如下所示:
private final static int TEXT_SIZE = 1000;
private final static int MATCHES_DIVISOR = 10;
private final static StringBuilder SOURCE
= new StringBuilder( randomNumeric( TEXT_SIZE ) );
MATCHES_DIVISOR
指示要注入的变量数量:
private void injectVariables( final Map<String, String> definitions )
for( int i = (SOURCE.length() / MATCHES_DIVISOR) + 1; i > 0; i-- )
final int r = current().nextInt( 1, SOURCE.length() );
SOURCE.insert( r, randomKey( definitions ) );
基准代码本身(JMH 似乎有些矫枉过正):
long duration = System.nanoTime();
final String result = testBorAhoCorasick( text, definitions );
duration = System.nanoTime() - duration;
System.out.println( elapsed( duration ) );
1,000,000 : 1,000
一个简单的微基准测试,包含 1,000,000 个字符和 1,000 个随机放置的字符串进行替换。
testStringUtils: 25 秒,25533 毫秒 testBorAhoCorasick: 0 秒,68 毫秒没有比赛。
10,000 : 1,000
使用 10,000 个字符和 1,000 个匹配字符串进行替换:
testStringUtils: 1 秒,1402 毫秒 testBorAhoCorasick: 0 秒,37 毫秒鸿沟缩小了。
1,000 : 10
使用 1000 个字符和 10 个匹配字符串进行替换:
testStringUtils: 0 秒,7 毫秒 testBorAhoCorasick: 0 秒,19 毫秒对于短字符串,设置 Aho-Corasick 的开销超过了 StringUtils.replaceEach
的蛮力方法。
可以使用基于文本长度的混合方法,以充分利用两种实现方式。
实现
考虑比较其他实现超过 1 MB 的文本,包括:
https://github.com/RokLenarcic/AhoCorasick https://github.com/hankcs/AhoCorasickDoubleArrayTrie https://github.com/raymanrt/aho-corasick https://github.com/ssundaresan/Aho-Corasick https://github.com/jmhsieh/aho-corasick https://github.com/quest-oss/Mensa论文
与算法相关的论文和资料:
http://www.cs.uku.fi/research/publications/reports/A-2005-2.pdf https://pdfs.semanticscholar.org/3547/ac839d02f6efe3f6f76a8289738a22528442.pdf http://www.ece.ncsu.edu/asic/ece792A/2009/ECE792A/Readings_files/00989753.pdf http://blog.ivank.net/aho-corasick-algorithm-in-as3.html【讨论】:
感谢用新的有价值的信息更新这个问题,这非常好。我认为 JMH 基准仍然是合适的,至少对于 10,000 : 1,000 和 1,000 : 10 之类的合理值(JIT 有时可以进行神奇的优化)。 删除builder.onlyWholeWords(),它的工作方式类似于字符串替换。 非常感谢您的出色回答。这绝对是非常有帮助的!我只是想评论一下,为了比较这两种方法,并给出一个更有意义的例子,在第二种方法中应该只构建一次 Trie,并将其应用于许多不同的输入字符串。我认为这是访问 Trie 与 StringUtils 的主要优势:您只需构建一次。不过,非常感谢您的回答。它很好地分享了实施第二种方法的方法【参考方案10】:这对我有用:
String result = input.replaceAll("string1|string2|string3","replacementString");
例子:
String input = "applemangobananaarefruits";
String result = input.replaceAll("mango|are|ts","-");
System.out.println(result);
输出: apple-banana-frui-
【讨论】:
正是我需要的朋友:) input.replaceAll("string1|string2|string3","replacementString1|replacementString2|replacementString3") 会很棒 以上所有选项中的最佳选择!【参考方案11】:总结:Dave 答案的单类实现,自动选择两种算法中最有效的。
这是基于上述优秀answer from Dave Jarvis 的完整的单类实现。该类自动在两种不同的提供算法之间进行选择,以实现最大效率。 (此答案适用于只想快速复制和粘贴的人。)
替换字符串类:
package somepackage
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.ahocorasick.trie.Emit;
import org.ahocorasick.trie.Trie;
import org.ahocorasick.trie.Trie.TrieBuilder;
import org.apache.commons.lang3.StringUtils;
/**
* ReplaceStrings, This class is used to replace multiple strings in a section of text, with high
* time efficiency. The chosen algorithms were adapted from: https://***.com/a/40836618
*/
public final class ReplaceStrings
/**
* replace, This replaces multiple strings in a section of text, according to the supplied
* search and replace definitions. For maximum efficiency, this will automatically choose
* between two possible replacement algorithms.
*
* Performance note: If it is known in advance that the source text is long, then this method
* signature has a very small additional performance advantage over the other method signature.
* (Although either method signature will still choose the best algorithm.)
*/
public static String replace(
final String sourceText, final Map<String, String> searchReplaceDefinitions)
final boolean useLongAlgorithm
= (sourceText.length() > 1000 || searchReplaceDefinitions.size() > 25);
if (useLongAlgorithm)
// No parameter adaptations are needed for the long algorithm.
return replaceUsing_AhoCorasickAlgorithm(sourceText, searchReplaceDefinitions);
else
// Create search and replace arrays, which are needed by the short algorithm.
final ArrayList<String> searchList = new ArrayList<>();
final ArrayList<String> replaceList = new ArrayList<>();
final Set<Map.Entry<String, String>> allEntries = searchReplaceDefinitions.entrySet();
for (Map.Entry<String, String> entry : allEntries)
searchList.add(entry.getKey());
replaceList.add(entry.getValue());
return replaceUsing_StringUtilsAlgorithm(sourceText, searchList, replaceList);
/**
* replace, This replaces multiple strings in a section of text, according to the supplied
* search strings and replacement strings. For maximum efficiency, this will automatically
* choose between two possible replacement algorithms.
*
* Performance note: If it is known in advance that the source text is short, then this method
* signature has a very small additional performance advantage over the other method signature.
* (Although either method signature will still choose the best algorithm.)
*/
public static String replace(final String sourceText,
final ArrayList<String> searchList, final ArrayList<String> replacementList)
if (searchList.size() != replacementList.size())
throw new RuntimeException("ReplaceStrings.replace(), "
+ "The search list and the replacement list must be the same size.");
final boolean useLongAlgorithm = (sourceText.length() > 1000 || searchList.size() > 25);
if (useLongAlgorithm)
// Create a definitions map, which is needed by the long algorithm.
HashMap<String, String> definitions = new HashMap<>();
final int searchListLength = searchList.size();
for (int index = 0; index < searchListLength; ++index)
definitions.put(searchList.get(index), replacementList.get(index));
return replaceUsing_AhoCorasickAlgorithm(sourceText, definitions);
else
// No parameter adaptations are needed for the short algorithm.
return replaceUsing_StringUtilsAlgorithm(sourceText, searchList, replacementList);
/**
* replaceUsing_StringUtilsAlgorithm, This is a string replacement algorithm that is most
* efficient for sourceText under 1000 characters, and less than 25 search strings.
*/
private static String replaceUsing_StringUtilsAlgorithm(final String sourceText,
final ArrayList<String> searchList, final ArrayList<String> replacementList)
final String[] searchArray = searchList.toArray(new String[]);
final String[] replacementArray = replacementList.toArray(new String[]);
return StringUtils.replaceEach(sourceText, searchArray, replacementArray);
/**
* replaceUsing_AhoCorasickAlgorithm, This is a string replacement algorithm that is most
* efficient for sourceText over 1000 characters, or large lists of search strings.
*/
private static String replaceUsing_AhoCorasickAlgorithm(final String sourceText,
final Map<String, String> searchReplaceDefinitions)
// Create a buffer sufficiently large that re-allocations are minimized.
final StringBuilder sb = new StringBuilder(sourceText.length() << 1);
final TrieBuilder builder = Trie.builder();
builder.onlyWholeWords();
builder.ignoreOverlaps();
for (final String key : searchReplaceDefinitions.keySet())
builder.addKeyword(key);
final Trie trie = builder.build();
final Collection<Emit> emits = trie.parseText(sourceText);
int prevIndex = 0;
for (final Emit emit : emits)
final int matchIndex = emit.getStart();
sb.append(sourceText.substring(prevIndex, matchIndex));
sb.append(searchReplaceDefinitions.get(emit.getKeyword()));
prevIndex = emit.getEnd() + 1;
// Add the remainder of the string (contains no more matches).
sb.append(sourceText.substring(prevIndex));
return sb.toString();
/**
* main, This contains some test and example code.
*/
public static void main(String[] args)
String shortSource = "The quick brown fox jumped over something. ";
StringBuilder longSourceBuilder = new StringBuilder();
for (int i = 0; i < 50; ++i)
longSourceBuilder.append(shortSource);
String longSource = longSourceBuilder.toString();
HashMap<String, String> searchReplaceMap = new HashMap<>();
ArrayList<String> searchList = new ArrayList<>();
ArrayList<String> replaceList = new ArrayList<>();
searchReplaceMap.put("fox", "grasshopper");
searchReplaceMap.put("something", "the mountain");
searchList.add("fox");
replaceList.add("grasshopper");
searchList.add("something");
replaceList.add("the mountain");
String shortResultUsingArrays = replace(shortSource, searchList, replaceList);
String shortResultUsingMap = replace(shortSource, searchReplaceMap);
String longResultUsingArrays = replace(longSource, searchList, replaceList);
String longResultUsingMap = replace(longSource, searchReplaceMap);
System.out.println(shortResultUsingArrays);
System.out.println("----------------------------------------------");
System.out.println(shortResultUsingMap);
System.out.println("----------------------------------------------");
System.out.println(longResultUsingArrays);
System.out.println("----------------------------------------------");
System.out.println(longResultUsingMap);
System.out.println("----------------------------------------------");
需要的 Maven 依赖项:
(如果需要,将这些添加到您的 pom 文件中。)
<!-- Apache Commons utilities. Super commonly used utilities.
https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
</dependency>
<!-- ahocorasick, An algorithm used for efficient searching and
replacing of multiple strings.
https://mvnrepository.com/artifact/org.ahocorasick/ahocorasick -->
<dependency>
<groupId>org.ahocorasick</groupId>
<artifactId>ahocorasick</artifactId>
<version>0.4.0</version>
</dependency>
【讨论】:
【参考方案12】:import java.util.*;
import java.io.*;
public class Main
public static void main(String[] args) throws IOException, Exception
// this program might help you with your problem
// if not, I still hope you could get some ideas out of this
Scanner userInput = new Scanner(System.in),
fileLocation = new Scanner(new File(userInput.nextLine())); // enter your .txt, .java etc.. file local directory
String search = userInput.nextLine().trim(), // the word or line you want to replace
replace = userInput.nextLine().trim(); // the word or line replacement
String newFile = ""; // this will be the template for your edited file
LinkedList<String> lineOfWords = new LinkedList<String>(); // every line of words or sentences will be stored in here
for (int index = 0; fileLocation.hasNextLine(); index++)
lineOfWords.add(fileLocation.nextLine().replaceAll(search, replace)); // it will edit the line if there was a match before storing it in the list
newFile = newFile.concat(lineOfWords.get(index)) + "\n"; // this will create an edited file
FileWriter saveNewFile = new FileWriter(userInput.nextLine()); // enter the local directory where you want your new file to be saved
saveNewFile.write(newFile); // finally, the saving method
saveNewFile.close(); // closing all these are necessary
fileLocation.close(); // closing all these are necessary
userInput.close(); // closing all these are necessarry
【讨论】:
以上是关于Java一次替换字符串中的多个不同子字符串(或以最有效的方式)的主要内容,如果未能解决你的问题,请参考以下文章
java 字符串替换函数replaceAll 一次同时替换多个字符串