No, I didn't say that. There are many ways of doing this but using the split() method is the easiest to code and understand.
The point is you shouldn't be worrying about performance efficiencies until you know there is a problem. And by 'know' I mean having analyzed the code using a profiling tool and not just because you think it might be a problem.
now: what's the best?
That depends on what you mean by 'Best'. Your code returns the items in reverse order - is that really you want?
Assuming you mean coding style (and you want the items in the correct order) I would do:
Code:
StringBuilder sb = new StringBuilder();
for ( int ii = 0; ii < 1000; ii++ ) {
sb.append("home" + ii + ".");
}
sb.setLength(sb.length() - 1);
String[] sp = sb.toString().split("\\.");
for ( int i = sp.length - 3; i < sp.length; i++ ) {
System.out.println(sp[i]);
}
Bookmarks