This discussion is archived
2 Replies Latest reply: Mar 12, 2013 3:33 AM by 800839 RSS

Multichar delimeter to split string

800839 Newbie
Currently Being Moderated
Hi,

Splitting a String with multichar delimeter:-


String input = "BOBMICHEAL$@$AR@12-5";
StringTokenizer strTokenizer = new StringTokenizer(input,"$@$");
String custName = null;
String custNumber = null;
List<String> custNameList =new ArrayList<String>();
List<String> custNumberList = new ArrayList<String>();
while(strTokenizer.hasMoreTokens()){
custName = strTokenizer.nextToken();
System.out.println("the custName is:"+custName);
custNameList.add(custName);
custNumber = strTokenizer.nextToken();
System.out.println("the custNumber is:"+custNumber);
custNumberList.add(custNumber);
}

Output:-

the custName is:BOBMICHEAL
the custNumber is:AR
the custName is:12-5

Here I have 2 data custname & custnumber, here the custname (BOBMICHEAL)and custnumber (AR@12-5) can have special characters.
We will get the custname & custnumber appended with a multichar delimeter $@$, hence we are using the delimeter $@$ to split the string.
But the problem is since we are having "@"for custnumber AR@12-5 the multichar delimeter $@$ is splitting the custnumber in to
2 as well (AR & 12-5) which is wrong. Here I need to get the values as below:-

the custName is:BOBMICHEAL
the custNumber is:AR@12-5

After the multichar delimiter $@$ we need to get the whole custnumber my supporting the special characters as well. Hence we need to get the custnumber
as AR@12-5 as supposed to AR & 12-5 as shown above. Now i can achieve this as there can be any Spl. characters that can be there for custname & number?

Please clarify.

Thanks.
  • 1. Re: Multichar delimeter to split string
    gimbal2 Guru
    Currently Being Moderated
    What the heck:
    public class Test {
    
      public static void main(String[] args){
         String test = "This is$@$a test for split$@$$@$, it works.";
         
         String[] parts = test.split("\\$@\\$");
         if(parts != null){
         
           for(int i = 0; i < parts.length; i++){
             System.out.println(parts);
         }
         
         } else {
         System.out.println("Blast! Failed!");
         }
    }
    }
    Output:
    This is
    a test for split

    , it works.
    I'm silly like that. When I want to split a String, I use String.split().                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
  • 2. Re: Multichar delimeter to split string
    800839 Newbie
    Currently Being Moderated
    Thanks gimbal2.

Legend

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