use can use constructor of the StringBuilder
StringBuilder builder = new StringBuilder(string.substring(start, end));
Answer from mss on Stack OverflowVideos
use can use constructor of the StringBuilder
StringBuilder builder = new StringBuilder(string.substring(start, end));
String implements CharSequence, so you can use StringBuffer.append(CharSequence s,int start, int end), however I would caution that unless you know that this is likely to be/is a problem, then using this method instead of substring is premature optimization.
A practical reason is that it makes things easier in some common situations:
- If you want the operation to include everything until the end of the string, you can directly use the length as
endIndex - If you have "separation characters", like the dot between base name and filetype suffix, you typically don't want to include them, so you can use the index of the separation character as
endIndex, e.g.String basename = filename.substring(0, filename.indexOf('.'))
I agree with Micheal Borgwardt's answer but there is an opportunity for elaboration. The approach you see here is not only useful but also consistent with most the APIs you will find in Java and many other languages. The association with 0-based indexing is more clear if you think about the standard old-style for-loop: for (int i = 0; i < end; i++). These days we tend to favor for-each style constructs but this is how a lot of code is written. Note the while condition, it's based on and exclusive end with a < instead of a <=. When you are writing a lot of loops like this, it's helpful to use a similar construct each time.
But to really understand the bigger picture on this, I think it helps to look at a different kind of problem: time intervals. Let's imagine I want to tell if something happened today, in the morning. If I structure this as an inclusive end, I then have a problem of figuring out what the last time possibly could be. That means I need to know the smallest resolution of the clock. Is it seconds, milliseconds, microseconds, nanoseconds? Let's say I think seconds is good enough. So I say 'less than or equal to 11:59'. Does that work if it happened at 11:59 and 30 seconds? Well, I'll check whether "the minute of the day was less than or equal to 11:59". Or I could just say "less than noon" which is a lot more elegant and tends to be more robust.
Or consider date periods. How do I know if two date periods are adjacent i.e. that there is no gap in between? If I used an exclusive end, it's trivial. I just check that the (exclusive) end on the first period is equal to or after the (inclusive) start of the second. If I use an inclusive end, now I have to get out a calendar and figure out if the day after September 30th is October 1st. And does March 1st follow Feb 28? What year is it? Divide that by 4 unless we are talking about the last year of the century but don't forget that the year 2000 is the exception to the exception. There are strategies to solve that kind of thing but the easiest is to convert the inclusive end to exclusive.