| Author |
String array -- how to split a string array based on a delimiter
|
Divya Kotamraju
Greenhorn
Joined: Oct 07, 2010
Posts: 21
|
|
Hello Everyone!!
I have a string array --> string[] arr={"CSE01A","ECE02B","MECH03C"};
Now I have to print course=CSE01, ECE02,MECH03
and grade= 'A','B','C'
I tried to use split() using the comma delimiter but that is not working for an array of strings..
Can someone please let me know how to do this..
Thank you!!
|
 |
Siddhesh Deodhar
Ranch Hand
Joined: Mar 05, 2009
Posts: 117
|
|
|
You can add delimiter between course and grade and than store to array.
|
Good, Better, Best, Don't take rest until, Good becomes Better, and Better becomes Best.
Sidd : (SCJP 6 [90%] )
|
 |
Maneesh Godbole
Saloon Keeper
Joined: Jul 26, 2007
Posts: 8441
|
|
Siddhesh Deodhar wrote:You can add delimiter between course and grade and than store to array.
No need to change the structure.
Check out the String API for a method which will let you retrieve a character at the specified index.
|
[Donate a pint, save a life!] [How to ask questions] [Onff-turn it on!]
|
 |
Divya Kotamraju
Greenhorn
Joined: Oct 07, 2010
Posts: 21
|
|
Thank you for the quick reply.
but I dont know the length of the string array and so when I enter the array at the command prompt then I need to split the coursegrades to course and grade. I have seen the string API but did not know which one to use. Can you please explain clearly.
Thank you!!
|
 |
Siddhesh Deodhar
Ranch Hand
Joined: Mar 05, 2009
Posts: 117
|
|
|
In that case you can use method suggested by Maneesh. Retrieve "grade" as last character of string "cousergrade".
|
 |
Maneesh Godbole
Saloon Keeper
Joined: Jul 26, 2007
Posts: 8441
|
|
You don't need to split the array at all. You can obtain the individual array elements using the index or an Iterator.
An individual array element in your case is a String.
Again check the String API for a method which will give you the length. The use my earlier suggestion to obtain the last character in the String.
Note: indexes start with 0 and not 1
In case you run into problems, post your code.
|
 |
 |
|
|
subject: String array -- how to split a string array based on a delimiter
|
|
|