Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Substring

 
Ale Lima
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys !

I have a doubt.

public static void main (String[] args ) {

StringBuffer s = new StringBuffer ("123456789");

System.out.println(" The value is [" + s + "]");

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.

the first position is not the index 0 ??


I know that this is basic, but I am confused!




 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alessandra,


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.
 
Ale Lima
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ruben
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
meera kanekal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Craig Taylor
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.
 
meera kanekal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But in a Zero based index

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
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)...
 
meera kanekal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all so much for clarifying this subject for me.
Meera
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic