• Post Reply Bookmark Topic Watch Topic
  • New Topic

Query related to getChars()  RSS feed

 
Vishal Hegde
Ranch Hand
Posts: 1078
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Output: The Character array equals: w are y


The getChars() method first value is 2 so

it starts from W

But the second value is 9 so it should show the output "yo", right???


Output: should be: w are yo (as o is in the 9th position)
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, the javadoc for String.getChars() says the second parameter is "the index after the last character in the string to copy".

http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#getChars%28int,%20int,%20char[],%20int%29

rc
 
Arek Sokolowski
Greenhorn
Posts: 8
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See also this piece of javadoc:
'The first character to be copied is at index srcBegin; the last character to be copied is at index srcEnd-1 (thus the total number of characters to be copied is srcEnd-srcBegin)'

In your case, 9-2=7; index srcEnd-1 gives 8, so this is your last index.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!