A brief overview of Java's string handling was presented in our previous posts:
- Post# 94 String Class
- Post# 95 Concatenate two strings using '+' operator
- Post# 96 Using equals( ) method for comparing two strings
- Post# 97 Using length( ) method for finding the length of a string
- Post# 98 Using charAt( ) method to retrieve the character from string text
- Post# 99 String Arrays
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)
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)
1. Create a class 'getCharsDemo' under any project as shown below:
3. Observe that the output is displayed in the console as shown below"
After looking at this table, you can figure out the output as 'demo' i.e. Index 10 to Index (14-1)
Index |
0
|
1
|
2
|
3
|
4
|
5
|
6
|
7
|
8
|
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:
Post a Comment