System.out.println(" delete all position between 0 and 3 [" + s.substring(0, 3) + "]" );
s.delete(0, 3);
System.out.println(" Now the value is " + s); // Here is 456789
System.out.println(" replace all position between 1 and 3 [" + s.substring(1, 3) + "]" );
s.replace(1, 3, "24");
System.out.println(" Now the value is " + s); // Here is 424789
}
I don't understand why this substring, s.substring(1, 3), took 2 positions.
The substring begins at the specified index and extends to the end of this sequence.
parameter
start - The beginning index, inclusive.
end - The ending index, exclusive.
s.replace(1, 3, "24");
This replaces the part of s between index 1 (inclusive) and index 3 (exclusive) by "24". That is, replaces characters at index 1 and 2 with 24, so you get 424789.
All code in my posts, unless a source is explicitly mentioned, is my own.
Ale Lima
Greenhorn
Joined: Jan 04, 2009
Posts: 15
posted
0
Thanks Ruben
Ruben Soto
Ranch Hand
Joined: Dec 16, 2008
Posts: 1032
posted
0
No problem, Alessandra! Remember that pretty much any indexable thing in Java (arrays, String instances, StringBuilder and StringBuffer instances) is indexed starting at 0 (I don't know if there is any exception to that.) Also, when you provide two indexes to specify a range, normally (again, I don't know if there is any exception) the first index will be inclusive and the last index will be exclusive (the index of the first element after the range itself.)
Ruben,
Could you please explain what you mean by ndex 1 (inclusive) and index 3 (exclusive)? I am a little confused by the terminology here.
Thanks,
Meera
Inclusive means that that value is considered in the operation, eg: a definition of 1 (inclusive) to 3 (exclusive) indicates that 1 should be considered, as it's inclusive, while 3 should not be considered (as it's exclusive).
Hence, the above example for integers would have a list of : 1, 2.
meera kanekal
Ranch Hand
Joined: Feb 13, 2005
Posts: 75
posted
0
My confusion has to do with the fact that in SCJP 5 by K&B in CH6 under the explanation for
public String subString(int begin, int end)
says "the first argument represents the starting location (zero based) of the substring. .... Unfortunately the ending argument is not zero based, so if the second argument is 7, the last character in the returned String will be in the original String's 7 position which is index 6 "
Could any one of you please clarify this?
ex: String x = "0123456789";
System.out.println(x.subString(5,8));
The output is "567"
0 1 2 3 4 5 6 7 8 9
^ ^ ^ ^ ^ ^ ^ ^ ^ ^
In this case position 5 should be 4 if the first argument is zero based. Then why is it 5?
In my opinion K&B should give a better explanation than confusing us.
Thanks,
Meera
x.substring(5,8)
what is the number at 0based index 5 : it's 5
what is the number at 1based index 8 : it's 7
so, "567" is returned. Use letters instead of numbers if you're still confused.
It may be easier to remember that substring returns a String which begins at the specified beginIndex and extends to the character at index endIndex - 1.
0,1,2,3,4 where 4 is in the 5th position. So does that mean that index and position are not the same? I am still confused.
Suppose we have
The output should be D E. ( D is in index 3 and E is in index 5. Another way to think as has been mentioned by Alessandra and Ruben is the first argument inclusive --> D , and second argument exclusive i.e. exclude F at index 5.)
Is this correct?
Thanks,
Meera
Another way to think as has been mentioned by Alessandra and Ruben is the first argument inclusive --> D , and second argument exclusive i.e. exclude F at index 5.)
Is this correct?
Yes. You can also think : first argument is inclusive, and take 8-5=3 characters.
The best way to think about this is as given at the end of the SCJP FAQ and in one of the explanation of inquisition. Just think of the indexes as being in between the characters. Just look at this string (ignore the space between characters)
Now if the substring method is like this substring(2,5);, then the resulting string would be CDE. Now you don't need to worry about 0 based and one based indexes. This applies to all the index based methods of String class and StringBuffer and String Builder class (eg. the delete method of StringBuffer and StringBuilder class)...