Is there any mistake in K&S book for strings .It is written that StringBuilders insert method is zero index based but it is not zero based it is 1 based.
Why are you thinking that its 1 based?
StringBuilder's insert method uses zero based index.
If we take the above example, the result would be 01hello234.
When we call insert(2,"hello") on str, it inserts "hello" at index 2(means 3rd character).
Refer to the Java API documentation for more information!
In a subtitle "Important methods in the StringBuffer and StringBuilder Classes":
under method delete() I found:
"...The starting index of the SUBSTRING to be removed is defined by the first argument (which is zero-based), and the ending index of the substring to be removed is defined by the second argument (but it is one-based)!..."
under method insert():
"...the offset is zero-based..."
Or is there any other places you saw that?
And I'm glad you asked it, cause I thought that StringBuffer/Builder is zero-based after I read the chapter (don't know why).
chetan dhumane wrote:I know that its is 1 based but read the book carefully.It is written that the index is zero based.
Hey I never said that its one based index . I gave the example to show that it is based on zero based index. Just read how indexing should be looked at here...
For String/StringBuilder/StringBuffer functions, can we say index parameter are all 0-based, but the first index will always be inclusive, and the second index(if present) are always exclusive?
Jia Tan wrote:For String/StringBuilder/StringBuffer functions, can we say index parameter are all 0-based, but the first index will always be inclusive, and the second index(if present) are always exclusive?
Jia, that's exactly the way I think about it, and I think it's the most logical and easiest way to remember how it works.
All code in my posts, unless a source is explicitly mentioned, is my own.
I hired a bunch of ninjas. The fridge is empty, but I can't find them to tell them the mission.