Me gustaría saber si hay un equivalente a tr /// (como se usa en Perl) en Java. Por ejemplo, si quería reemplazar todos "s" s con s "p" en "Mississippi" y viceversa, podría, en Perl, escribir¿hay alguna manera de usar tr /// (o equivalente) en java?
#shebang and pragmas snipped...
my $str = "mississippi";
$str =~ tr/sp/ps/; # $str = "mippippissi"
print $str;
La única manera que puedo pensar para hacerlo en Java es utilizar un personaje ficticio con el método String.replace()
, es decir
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);
¿hay una mejor manera de hacer esto?
Gracias de antemano.
este post podría ayudar http://stackoverflow.com/questions/ 3254358/replace-char-to-char-in-a-string –