Java:将字符串中的特定char替换为索引

时间:2016-06-18 19:38:14

标签: java string replace complexity-theory

有没有更快的方法来替换$ som $中的$ 到0som4thing(将$更改为其索引) 其他然后for循环和charAt(i)

2 个答案:

答案 0 :(得分:0)

除了在Stream上使用mapper而不是for-loop之外,我还不知道如何做到这一点。通常情况下,我的体验中的流更快一点,但这可能有点主观。

答案 1 :(得分:0)

I cannot think of anything that would be magnitudes faster (and I'm pretty sure this really is premature optimization), but using an enhanced for loop might buy some milliseconds in range checks - I just think it's not worth using it in this case.The already mentioned StringBuffer might actually make the bigger difference. Just stick with the standard for loop, alternatives might or might not be faster, but probably less readable.