This post originated from an RSS feed registered with Java Buzz
by dion.
Original Post: Java 5: String.regionMatches()
Feed Title: techno.blog(Dion)
Feed URL: http://feeds.feedburner.com/dion
Feed Description: blogging about life the universe and everything tech
Kirk talks about the new regionMatches(..) methods available in the Java 5 String class.
There were questions on readability, and Kirk has a benchmark to proove the micro-performance.
I think the methods definitely harder to grok at first, and there is room for helper methods (e.g. in the first case: foo.startsWithIgnoreCase(bar)).
Case-folding startsWith()
If youve ever written code like
if (foo.toLowerCase().startsWith(bar.toLowerCase())) ...
and winced, because you were creating and throwing away two Strings, then rejoice! Now you may
if (foo.regionMatches(true, 0, bar, 0, bar.length())) ...
Comparison of arbitrary string regions
Before:
if (foo.equals(bar.substring(m))) ...
After:
if (foo.regionMatches(0, bar, m, bar.length() - m)) ...