This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes String array -- how to split a string array based on a delimiter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String array -- how to split a string array based on a delimiter" Watch "String array -- how to split a string array based on a delimiter" New topic
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: 10167
    
    8

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.


[How to ask questions] [Donate a pint, save a life!] [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: 10167
    
    8

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
 
Similar Threads
String split - Regular expression
How we can Count the no.of words in a given String
splitting arrays
parsing date as a string
Split()-method