• Post Reply Bookmark Topic Watch Topic
  • New Topic

dstBegin explanation  RSS feed

 
Greenhorn
Posts: 13
Firefox Browser Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1) if i wanna print the whole string than what should i write instead of chars1[1] either by passing the chars1 in to string?? or by any other means

2) i am not getting what dstBegin is?? if i write 0,1,2 at dstBegin it works but as soon as i write 3 it throws exception.

dstbegin + (srcEnd-srcBegin) - 1 ???
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The getChars() method is a method of class java.lang.String. You can look it up in the API documentation, where it is explained exactly what the arguments mean. Is there anything specific in this description that you don't understand?
API documentation wrote:
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)

Copies characters from this string into the destination character array.

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). The characters are copied into the subarray of dst starting at index dstBegin and ending at index:

dstbegin + (srcEnd-srcBegin) - 1

Parameters:

srcBegin - index of the first character in the string to copy.
srcEnd - index after the last character in the string to copy.
dst - the destination array.
dstBegin - the start offset in the destination array.

Throws:

IndexOutOfBoundsException - If any of the following is true:
srcBegin is negative.
srcBegin is greater than srcEnd
srcEnd is greater than the length of this string
dstBegin is negative
dstBegin+(srcEnd-srcBegin) is larger than dst.length

 
Arvind Kurmi
Greenhorn
Posts: 13
Firefox Browser Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:The getChars() method is a method of class java.lang.String. You can look it up in the API documentation, where it is explained exactly what the arguments mean. Is there anything specific in this description that you don't understand?

yaa i wanna understand about destBegin ... how it works ... how we can specify its value ...
even i don't understand dis sentence dstbegin + (srcEnd-srcBegin) - 1
if suppose my getChars() has srcBegin = 1 srcEnd = 6 then what the values i should give to destBegin
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, as the documentation says, the getChars() methods copies part of the characters in the string to a char array. You pass the array where you want the chars copied to as an argument to the method (the dst argument). You can also specify where in the dst array you want the method to put the characters. That's what you specify with the dstBegin argument.

For example, suppose that you have a char array with 10 elements (index 0 upto 9), with all elements set to 'x'. It will look like this:

+-+-+-+-+-+-+-+-+-+-+
|0|1|2|3|4|5|6|7|8|9|
+-+-+-+-+-+-+-+-+-+-+
|x|x|x|x|x|x|x|x|x|x|
+-+-+-+-+-+-+-+-+-+-+

Now, suppose that you have a string with the content "hello" and you want to copy it to the char array from index 2, so that the resulting char array will look like this:

+-+-+-+-+-+-+-+-+-+-+
|0|1|2|3|4|5|6|7|8|9|
+-+-+-+-+-+-+-+-+-+-+
|x|x|h|e|l|l|o|x|x|x|
+-+-+-+-+-+-+-+-+-+-+

Then you would call getChars() like this:

So, dstBegin = 2, which means that it will start copying at index 2 in the destination array.

With regard to dstbegin + (srcEnd-srcBegin) - 1: that's the index of the last character that's written in the array, just simple math. In the example above, srcBegin = 0, srcEnd = 5 so this will become 2 + (5 - 0) - 1 = 6, which is the index of the 'o' in the resulting array.
 
Arvind Kurmi
Greenhorn
Posts: 13
Firefox Browser Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
For example, suppose that you have a char array with 10 elements (index 0 upto 9), with all elements set to 'x'. It will look like this:

+-+-+-+-+-+-+-+-+-+-+
|0|1|2|3|4|5|6|7|8|9|
+-+-+-+-+-+-+-+-+-+-+
|x|x|x|x|x|x|x|x|x|x|
+-+-+-+-+-+-+-+-+-+-+

Now, suppose that you have a string with the content "hello" and you want to copy it to the char array from index 2, so that the resulting char array will look like this:

+-+-+-+-+-+-+-+-+-+-+
|0|1|2|3|4|5|6|7|8|9|
+-+-+-+-+-+-+-+-+-+-+
|x|x|h|e|l|l|o|x|x|x|
+-+-+-+-+-+-+-+-+-+-+



With regard to dstbegin + (srcEnd-srcBegin) - 1: that's the index of the last character that's written in the array, just simple math. In the example above, srcBegin = 0, srcEnd = 5 so this will become 2 + (5 - 0) - 1 = 6, which is the index of the 'o' in the resulting array.


THIS WAS REALLY HELPFULL THANKS A LOT... AWESOME EXPLANATION
 
Arvind Kurmi
Greenhorn
Posts: 13
Firefox Browser Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i wanna ask one thing toCharArray returns whole string if i wanna show then what should i write...
did i need to declare a string and pass the value in that to get the whole content in it. rather than a single character ???
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arvind Kurmi wrote: . . . getCharArray returns whole string . . .
No, it doesn't. It returns a char[] equal in size and contents to the String. To print it, you can try the methods of )]the Arrays class, or write your own method.
 
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And it's called toCharArray(), not getCharArray().
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!