What is an efficient way to replace many characters in a string?

前端 未结 9 1045
梦谈多话
梦谈多话 2020-12-28 14:31

String handling in Java is something I\'m trying to learn to do well. Currently I want to take in a string and replace any characters I find.

Here is my current inef

相关标签:
9条回答
  • 2020-12-28 14:53

    Any time we have problems like this we use regular expressions are they are by far the fastest way to deal with what you are trying to do.

    Have you already tried regular expressions?

    0 讨论(0)
  • 2020-12-28 14:54

    Use the function String.replaceAll. Nice article similar with what you want: link

    0 讨论(0)
  • 2020-12-28 14:57

    My first choice would be to use a StringBuilder because you need to remove some chars from the string.

    Second choice would be to iterate throw the array of chars and add the treated char to another array of the inicial size of the string. Then you would need to copy the array to trim the possible unused positions.

    After that, I would make some performance tests to see witch one is better.

    0 讨论(0)
提交回复
热议问题