Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

quick java help please

Coder in-training
Joined
Dec 28, 2008
Messages
595
Reaction score
47
okay so say
string word = "ragezone";

and then it had a line that was: System.out.println(word.substring(2, word.length()-2));

shouldn't the output be:
gez instead of gezo
because you subtract one more since length doesn't include the 0 index?
 
Custom Title Activated
Member
Joined
Aug 21, 2009
Messages
1,149
Reaction score
598
It's 'gezo'. Why?

This are the index of the characters of the word "RaGEZONE"

R = 0
a = 1
G = 2
E = 3
Z = 4
O = 5
N = 6
E = 7

So, you printed with this line:

PHP:
System.out.println(word.substring(2, word.length()-2));

The word will start in index 2. Which is 'G' and will ends in the length of the word minus 2. So...

The length of the word "RaGEZONE" is 8, as it has 8 characters. 8 - 2 = 6.

So basically, the code line you're executing is the following:

PHP:
System.out.println("RaGEZONE".substring(2, 6));

Saying this. The beginning index (2) is inclusive, the final index (6) is exclusive. Saying this, the 6th index of the word won't be included in the word.

And that's why it's 'GEZO'.
 
Upvote 0
Back
Top