替换字符串中的许多字符的有效方法是什么? [英] What is an efficient way to replace many characters in a string?
问题描述
Java中的字符串处理是我努力学习做得好的事情。目前我想接受一个字符串并替换我找到的任何字符。
String handling in Java is something I'm trying to learn to do well. Currently I want to take in a string and replace any characters I find.
这是我目前效率低下的(有点愚蠢的IMO)功能。它被写成只是工作。
Here is my current inefficient (and kinda silly IMO) function. It was written to just work.
public String convertWord(String word)
{
return word.toLowerCase().replace('á', 'a')
.replace('é', 'e')
.replace('í', 'i')
.replace('ú', 'u')
.replace('ý', 'y')
.replace('ð', 'd')
.replace('ó', 'o')
.replace('ö', 'o')
.replaceAll("[-]", "")
.replaceAll("[.]", "")
.replaceAll("[/]", "")
.replaceAll("[æ]", "ae")
.replaceAll("[þ]", "th");
}
我跑了1.000.000次,耗时8182ms。那么我应该如何继续更改此功能以提高效率?
I ran 1.000.000 runs of it and it took 8182ms. So how should I proceed in changing this function to make it more efficient?
找到解决方案:
转换函数到这个
public String convertWord(String word)
{
StringBuilder sb = new StringBuilder();
char[] charArr = word.toLowerCase().toCharArray();
for(int i = 0; i < charArr.length; i++)
{
// Single character case
if(charArr[i] == 'á')
{
sb.append('a');
}
// Char to two characters
else if(charArr[i] == 'þ')
{
sb.append("th");
}
// Remove
else if(charArr[i] == '-')
{
}
// Base case
else
{
sb.append(word.charAt(i));
}
}
return sb.toString();
}
运行此功能1.000.000次需要518ms。所以我觉得这很有效率。感谢帮助人员:)
Running this function 1.000.000 times takes 518ms. So I think that is efficient enough. Thanks for the help guys :)
推荐答案
您可以创建一个String []表,其长度为Character.MAX_VALUE。 (包括映射到小写)
You could create a table of String[] which is Character.MAX_VALUE in length. (Including the mapping to lower case)
由于替换变得更复杂,执行它们的时间将保持不变。
As the replacements got more complex, the time to perform them would remain the same.
private static final String[] REPLACEMENT = new String[Character.MAX_VALUE+1];
static {
for(int i=Character.MIN_VALUE;i<=Character.MAX_VALUE;i++)
REPLACEMENT[i] = Character.toString(Character.toLowerCase((char) i));
// substitute
REPLACEMENT['á'] = "a";
// remove
REPLACEMENT['-'] = "";
// expand
REPLACEMENT['æ'] = "ae";
}
public String convertWord(String word) {
StringBuilder sb = new StringBuilder(word.length());
for(int i=0;i<word.length();i++)
sb.append(REPLACEMENT[word.charAt(i)]);
return sb.toString();
}
这篇关于替换字符串中的许多字符的有效方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!