Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is an efficient way to replace many characters in a string?

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.

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"); } 

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?

Solution found:

Converting the function to this

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(); } 

Running this function 1.000.000 times takes 518ms. So I think that is efficient enough. Thanks for the help guys :)

like image 268
Ólafur Waage Avatar asked Mar 29 '11 09:03

Ólafur Waage


People also ask

How do I replace multiple characters in a string?

Use the replace() method to replace multiple characters in a string, e.g. str. replace(/[. _-]/g, ' ') . The first parameter the method takes is a regular expression that can match multiple characters.

How do you replace characters in a string?

The Java string replace() method will replace a character or substring with another character or string. The syntax for the replace() method is string_name. replace(old_string, new_string) with old_string being the substring you'd like to replace and new_string being the substring that will take its place.


1 Answers

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(); }  
like image 173
Peter Lawrey Avatar answered Sep 22 '22 09:09

Peter Lawrey