2 Replies Latest reply: Mar 12, 2013 5:33 AM by 800839 RSS

    Multichar delimeter to split string


      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>();
      custName = strTokenizer.nextToken();
      System.out.println("the custName is:"+custName);
      custNumber = strTokenizer.nextToken();
      System.out.println("the custNumber is:"+custNumber);


      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.

        • 1. Re: Multichar delimeter to split string
          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++){
               } else {
               System.out.println("Blast! Failed!");
          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
            Thanks gimbal2.