相关疑难解决方法(0)

有没有办法在java中使用tr ///(或等效)?

我想知道Java中是否存在等价于tr ///(在Perl中使用的).例如,如果我想用"密西西比"中的"p"替换所有"s",反之亦然,我可以在Perl中写

#shebang and pragmas snipped...
my $str = "mississippi";
$str =~ tr/sp/ps/;  # $str = "mippippissi"
print $str;
Run Code Online (Sandbox Code Playgroud)

我能想到用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 …
Run Code Online (Sandbox Code Playgroud)

java tr str-replace

14
推荐指数
1
解决办法
3053
查看次数

替换为数组

即时尝试从数组中替换字符串,但这不起作用

dna[i].replace('T', 'C');
Run Code Online (Sandbox Code Playgroud)

用这种方式工作?

"ATCTA".replace('T', 'C');
Run Code Online (Sandbox Code Playgroud)

为什么不使用数组,我怎么可以使用数组[]中的替换

现在我有其他问题,我想在原始字符串中使用各种替换,我怎么能这个?

java

4
推荐指数
1
解决办法
6万
查看次数

标签 统计

java ×2

str-replace ×1

tr ×1