Google+

133. Using getChars( ) to extract more than one character from String Text






A brief overview of Java's string handling was presented in our previous posts:
Its time to explore more on Java's string handling. To start with, we've to understand the getChars( ) method which is used to extract more than one character at a time from String text.

getChars( ) 's syntax:

getChars(int sourceStart, int sourceEnd, char target[], int targetStart)
  • sourceStart - Index of the first character to be extracted in the source string (i.e. The characters will be extracted from the source string from the provided index). If the index is specified as 10 
  • sourceEnd - If the index is specified as 14 then the characters will be extracted till Index 13 only
  • target[] - It will store the extracted characters
  • targetStart - Index of the target array at which the extracted text need to be copied
Lets implement this on Eclipse IDE:

1. Create a class 'getCharsDemo' under any project as shown below:



2. Save and Run the 'getCharsDemo' class
3. Observe that the output is displayed in the console as shown below"





After looking at this output is very clear that, getChars( ) method started extracting the characters from Index  10  and extracted the characters till Index (14-1).

After looking at this table, you can figure out the output as 'demo' i.e. Index 10 to Index (14-1)               

Index
 9
10 
11
12
13
14
15
16
17
18
19
20
Character
T
h
i
s

i
s
  
a
d
e
m
o
s
t
r
i
n
g

Download this project:

Click here to download the project containing the class file used in this post (You can download this project and import into Eclipse IDE on your machine)



Please comment below to feedback or ask questions.

Using equalsIgnoreCase( ) method to compare two string irrespective of their case will be explained in the next post.


No comments: