Best code to Tokenise a string and get values dynamically(means if String(eg:CaseType)location is changed(i.e position moved frm midle position to last position in below string),that time also u get 'CaseType' value)at run time..in Android

String str="FN:David$@$LN:Abelson$@$CaseID:59$@$CaseType:2$@$TicketNumber:1529-SC-2011-003352";     
       StringTokenizer st = new StringTokenizer(str, "$@$");
       System.out.println("Tokens Count:  " + st.countTokens());
       String arrayString[]=new String[st.countTokens()];
       String ticketNumbr=null,caseId=null,caseType=null;
       int tokenCount=st.countTokens();
       for(int i=0;i<tokenCount;i++)
       {
           arrayString[i]=st.nextToken();
           System.out.println("###"+arrayString[i]);
          
       }
       for(int i=0;i<arrayString.length;i++){
          
           StringTokenizer subStrToken = new StringTokenizer(arrayString[i], ":");
           //Here 2-reprasents two tokens in arrayString[i]="FN:David" i.e "FN,David"
           String tokenString=subStrToken.nextToken();
           switch(tokenString.length()){
            
           case 6:
               caseId=subStrToken.nextToken();
               System.out.println("++caseId"+caseId);              
               break;
              
           case 8:
               caseType=subStrToken.nextToken();
               System.out.println("++caseType"+caseType);                  
               break;
           case 12:
               ticketNumbr=subStrToken.nextToken();
               System.out.println("++ticketNumbr"+ticketNumbr);
               break;
           default:continue;          
           }         
       }

Comments

Popular posts from this blog

Custom Dialog with no title and full screen in Android

Best working code to set Date limits on Date Picker Dialog,in Android

code to display .pdf,.text,.DOC,.DOCX,.doc extension files exists in SDCard