• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String array -- how to split a string array based on a delimiter

 
Divya Kotamraju
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can add delimiter between course and grade and than store to array.
 
Maneesh Godbole
Saloon Keeper
Posts: 11061
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Divya Kotamraju
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case you can use method suggested by Maneesh. Retrieve "grade" as last character of string "cousergrade".
 
Maneesh Godbole
Saloon Keeper
Posts: 11061
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic