字符串截取

来源:互联网 发布:淘宝村服务站怎样申请 编辑:程序博客网 时间:2024/04/29 10:23
  1. package cn.youpaoo;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. /** 
  6.  * 截取字符串(投票程序中的一部分) 
  7.  * @author yaowenchao 
  8.  * @since 2009-10-27 
  9.  */  
  10. public class SplitString {  
  11.       
  12.     public void splitSt(String str){  
  13.         //以分号把字符串转化为字符数组  
  14.         String[] strArray = str.split(";");  
  15.           
  16.         //从第一个数组元素中得到所要的值  
  17.         Integer surveyId = Integer.valueOf(strArray[0]);  
  18.           
  19.         //把字符数组的第一个元素去掉  
  20.         ArrayList strArrayList = new ArrayList();  
  21.         for(int i = 1; i < strArray.length; i++){  
  22.             strArrayList.add(strArray[i]);  
  23.         }  
  24.         //System.out.println(strArrayList.size());  
  25.         for (int j = 0 ; j < strArrayList.size(); j++){  
  26.             //得到问题部分  
  27.             String tmpParm = (String)strArrayList.get(j);  
  28.             String[] subject = tmpParm.split(":");  
  29.             String subjectParm = (String)subject[0];  
  30.             String[] subjectSplit = subjectParm.split("-");  
  31.             String subjectId = (String)subjectSplit[0];  
  32.             String subjectType = (String)subjectSplit[1];  
  33.               
  34.             //得到选项部分  
  35.             String optionParm = (String)subject[1];  
  36.             //从选项数组中拆分开每一个选项元素  
  37.             String[] optionSplit = optionParm.split(",");  
  38.             for(int l = 0; l < optionSplit.length; l++){  
  39.                 //得到某一个具体的选项  
  40.                 String optionObj = (String)optionSplit[l];  
  41.                 //拆分选项  
  42.                 String[] optionValue = optionObj.split("-");  
  43.                 //如果选项为问答题的答案  
  44.                 if(1 == optionValue.length){  
  45.                     String optionCont = optionValue[0];  
  46.                     System.out.println("问题的ID为:" + subjectId  
  47.                             + " 问题的类型为:" + subjectType + "问题的答案为:" + optionCont);  
  48.                 } else {  
  49.                     String optionId = optionValue[0];  
  50.                     String optionSort = optionValue[1];  
  51.                     System.out.println("问题的ID为:" + subjectId  
  52.                             + " 问题的类型为:" + subjectType + "问题的选项ID为: " + optionId  
  53.                             + " 选项的排序为:" + optionSort);  
  54.                 }  
  55.             }  
  56.               
  57.         }  
  58.     }  
  59.       
  60.     public static void main(String[] args){  
  61.         SplitString s = new SplitString();  
  62.         String str = "5;12-0:10-A;2-1:34-B,天涯;3-0:4-12";  
  63.         s.splitSt(str);  
  64.     }  
  65.   
  66. }