This discussion is archived
4 Replies Latest reply: Feb 1, 2011 8:03 AM by 800411 RSS

Concatenate and split up string

800411 Newbie
Currently Being Moderated
Hi All

Is there some how we can split up the string like this "1 |~| Diego|Maradona |~| Footballer|Argentina"

into the array of 3 elements: "1" , "Diego|Maradona" , "Footballer|Argentina"

Here is my code
public static void main(String args[]){
          System.out.println("========USE SPLIT========== " );
               String data = "1 |~| Diego|Maradona |~| Footballer|Argentina";
             String[] items = data.split(" |~| ");
             for (String item : items)
             {
                  System.out.println("item = " + item);
             }
             StringTokenizer tok = new StringTokenizer(data," |~| ");
             System.out.println("========USE TOKENIZER========== " );
             while(tok.hasMoreElements()){
                  System.out.println("item = " + tok.nextToken());
             }
          
     }
and the out put is:
========USE SPLIT==========
item = 1
item = |
item = |
item = Diego|Maradona
item = |
item = |
item = Footballer|Argentina
========USE TOKENIZER==========
item = 1
item = Diego
item = Maradona
item = Footballer
item = Argentina

Edited by: mycoffee on Feb 1, 2011 6:49 AM
  • 1. Re: Concatenate and split up string
    JoachimSauer Journeyer
    Currently Being Moderated
    split() takes a regular expression. "|" has a special meaning in regular expressions.

    Try using
    " \\|~\\| "
    as the argument to split()
  • 2. Re: Concatenate and split up string
    800411 Newbie
    Currently Being Moderated
    Thank you man
    public static void main(String args[]){
              System.out.println("========USE SPLIT========== " );
                   String data = "1 |~| Diego|Maradona |~| Footballer|Argentina";
                 String[] items = data.split(" \\|~\\| ");
                 for (String item : items)
                 {
                      System.out.println("item = " + item);
                 }
                 StringTokenizer tok = new StringTokenizer(data," \\|~\\| ");
                 System.out.println("========USE TOKENIZER========== " );
                 while(tok.hasMoreElements()){
                      System.out.println("item = " + tok.nextToken());
                 }
              
         }
    output:
    ========USE SPLIT==========
    item = 1
    item = Diego|Maradona
    item = Footballer|Argentina
    ========USE TOKENIZER==========
    item = 1
    item = Diego
    item = Maradona
    item = Footballer
    item = Argentina

    so SPLIT is my solution. Thanks
  • 3. Re: Concatenate and split up string
    JoachimSauer Journeyer
    Currently Being Moderated
    mycoffee wrote:
    so SPLIT is my solution. Thanks
    Try to understand what you do. The String you pass to the StringTokenizer is actually a list of characters on which to split. So it will split on each space, pipe or tilde.
  • 4. Re: Concatenate and split up string
    800411 Newbie
    Currently Being Moderated
    Yes
    I understood how it works like that before I posted here but I didnt know how to pass the string with the bars (||) as delimiter . I just tried to get my solution and your suggestion is great !!!!!!!!!

    Edited by: mycoffee on Feb 1, 2011 8:03 AM

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points