Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Performance of String replacements

 
Michael Laufer
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have to substitute all cariage returns and line feeds in a String object with a space character.
According to the Javadoc API of String are two methods available for such a task. Do you have any idea which method is the more appropriate for my purpose?

replace(char oldChar, char newChar)

or

replaceAll(String regex, String replacement)

Probably the first one is faster, or?

Kind regards & thanks in advance,

Michel
 
Rahul Mahindrakar
Ranch Hand
Posts: 1869
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael

Have you tried testing both these methods with a simple program for example that counts the time it takes for doing replacement and see which one is better.
 
Michael Laufer
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, just wrote such a short program and the result is that the first one is much faster!

Thanks
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How much faster was it? Performance might not be an issue if it isn't called often or is not the bottleneck. Still if the faster one does that job that is hard to argue with...
 
The moustache of a titan! The ad of a flea:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!