-6
public static void main(String[] args) {
    // TODO Auto-generated method stub

    String a="Apple-banana-pineapple";

    StringTokenizer st = new StringTokenizer(a,"-"); 

    String key = st.nextToken(); 
    String val = st.nextToken(); 
    System.out.println("key"+key);
    System.out.println("Values"+val);
}
Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
  • 1
    Hello, and Welcome to StackOverflow. Request you to please explain your problem properly. Describe what issues are you facing. What output did you expect? and What output you got. And also, please add the question to the question body also. And if possible, please post a compilable code. Thanks :) – Rohit Jain Feb 17 '14 at 05:10
  • You just asked an answer ?? where is the question ? – Suresh Atta Feb 17 '14 at 05:11
  • did you try to split using split('-') and get the data in an array – Saravanan Feb 17 '14 at 05:11
  • i need o/p like this 1.apple 2.banana 3.pineapple – kim.ramesh Feb 17 '14 at 05:12
  • 1
    @kim.ramesh Please edit your question. There is an `edit` link below it. Click it. – Rohit Jain Feb 17 '14 at 05:13

5 Answers5

1

You can try with split()

   String a="Apple-banana-pineapple";
    String[] arr=a.split("-");
    for(String i:arr){
        System.out.println(i);
    }

Out put:

   Apple
   banana
   pineapple
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
1

You can use the split("-") method of the String class.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
Balayesu Chilakalapudi
  • 1,386
  • 3
  • 19
  • 43
1
String[] parts = a.split("-");
BassT
  • 819
  • 7
  • 22
1

Use String.split() function.

String arr[]=a.split("-");
Rohan
  • 3,068
  • 1
  • 20
  • 26
0

You can use string.split to accomplish what you want. Here is the api:http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split(java.lang.String)

pengfeil
  • 36
  • 3