php strtr equivalent in java



Is there a function in Java which is equivalent to php’s strtr?

string strtr ( string $str , string $from , string $to )

If given three arguments, strtr function returns a copy of string where all occurrences of each (single-byte) character in $from have been translated to the corresponding character in $to, i.e., every occurrence of $from[$n] has been replaced with $to[$n], where $n is a valid offset in both arguments.





Cannot find a direct equivalence to your strtr in the JDK. In Apache Commons StringUtils, there is StringUtils.replaceChars

String.replace Does not really fit your case, unless you use it for each letter in your $from argument and replace it with the $to substitute. It’s pretty easy to implement your own utility function using StringBuilder and replacing it yourself, though. Which is pretty much what StringUtils.replace does.


Facebook Comments

Post a comment