Java で (Perl で使用されている) tr/// に相当するものがあるかどうかを知りたいです。たとえば、"mississippi" のすべての "s" を "p" に、またはその逆に置き換えたい場合、Perl では次のように記述できます。
#shebang and pragmas snipped...
my $str = "mississippi";
$str =~ tr/sp/ps/; # $str = "mippippissi"
print $str;
Javaでそれを行うために私が考えることができる唯一の方法は、String.replace()メソッドでダミー文字を使用することです。
String str = "mississippi";
str = str.replace('s', '#'); // # is just a dummy character to make sure
// any original 's' doesn't get switched to a 'p'
// and back to an 's' with the next line of code
// str = "mi##i##ippi"
str = str.replace('p', 's'); // str = "mi##i##issi"
str = str.replace('#', 'p'); // str = "mippippissi"
System.out.println(str);
これを行うより良い方法はありますか?
前もって感謝します。