title: How to replace characters in a java String?
body: <p>I like to replace a certain set of characters of a string with a corresponding replacement character in an efficent way. </p>
  
  <p>For example:</p>
  
  <pre><code>String sourceCharacters = "šđćčŠĐĆČžŽ";
  String targetCharacters = "sdccSDCCzZ";
  
  String result = replaceChars("Gračišće", sourceCharacters , targetCharacters );
  
  Assert.equals(result,"Gracisce") == true;
  </code></pre>
  
  <p>Is there are more efficient way than to use the <code>replaceAll</code> method of the String class?</p>
  
  <p>My first idea was:</p>
  
  <pre><code>final String s = "Gračišće";
  String sourceCharacters = "šđćčŠĐĆČžŽ";
  String targetCharacters = "sdccSDCCzZ";
  
  // preparation
  final char[] sourceString = s.toCharArray();
  final char result[] = new char[sourceString.length];
  final char[] targetCharactersArray = targetCharacters.toCharArray();
  
  // main work
  for(int i=0,l=sourceString.length;i&lt;l;++i)
  {
    final int pos = sourceCharacters.indexOf(sourceString[i]);
    result[i] = pos!=-1 ? targetCharactersArray[pos] : sourceString[i];
  }
  
  // result
  String resultString = new String(result);
  </code></pre>
  
  <p>Any ideas?</p>
  
  <p>Btw, the UTF-8 characters are causing the trouble, with US_ASCII it works fine.</p>