使用split()函数,把整个字符串分成"x=y"然后再次使用split分解出x和y

解决方案 »

  1.   

    使用split()函数,把整个字符串分成"x=y"然后再次使用split分解出x和y
      

  2.   

    String[] temp1 = str.split("&");
    for(int i=0;i<temp1.length;i++){
    String[] temp2 = temp1[i].split("=");
    System.out.println(temp2[0]);
    System.out.println(temp2[1]);
    }
      

  3.   

    // 完整的C类实现。
    public class C {
      private Map map = null;
      
      public C(String str) {
        map = new HashMap();
        splitStr(str);
      }
      
      private void splitStr(String str){
        StringTokenizer st = new StringTokenizer(str, "&");
        while(st.hasMoreTokens()){
          String s = st.nextToken();
          int index = s.indexOf("=");
          String key = s.substring(0, index);
          String value = s.substring(index+1);
          map.put(key, value);
        }
      }
      
      public String getValue(String param){
        Object value = map.get(param);
        if(value != null){
          return value.toString();
        }else {
          return null;
        }
      }}
      

  4.   

    class C {
        private String str;    public C(String str) {
             this.str = str;
        }    // -1 代表没有找到
        public int getValue(String s) {
            int rValue = -1;
            String tmpStr = s + "=";
            try {
              if (str != null && !str.equals("")) {
                int pos1 = str.indexOf(tmpStr);
                String subStr = str.substring(pos1 + tmpStr.length(), str.length());
                int pos2 = subStr.indexOf("&");
                pos2 = pos2 == -1 ? subStr.length() : pos2;
                rValue = Integer.parseInt(subStr.substring(0, pos2));
              }
              return rValue;
            }
            catch(NumberFormatException e) {
              return rValue;
            }
        }
        
        public static void main(String[] args) {
          C insC = new C("aa=12&bb=34&cc=56");
          System.out.println(insC.getValue("aa"));
          System.out.println(insC.getValue("cc"));
          System.out.println(insC.getValue("bb"));
          System.out.println(insC.getValue("dd"));
        }
    }
      

  5.   

    你的结构用spit()函数和StringTokenizer st = new StringTokenizer(str, "&");
    都可以来实现。看你自己的处理!
      

  6.   

    简单写了一个,要求你的JDK要1.4以上import java.util.regex.*;
    public class TTest {
      public static void main(String[] args) {
        String str = "a_=3&b=4&c=5&d=7";
        String[] strs=str.split("&");
        for(int i=0;i<strs.length;i++){
          //通过正则表达式分解字符串
          Pattern pattern = Pattern.compile("([\\w]*)=([\\w]*)");
          Matcher matcher = pattern.matcher(strs[i]);
                while (matcher.find()){
                  String key=matcher.group(1);
                  String value=matcher.group(2);
                  System.out.println(key+":"+value);
                }
        }  }
    }
      

  7.   

    非常感谢Apollo47(阿波罗)与esunboy(天使联盟),两个不同的解决办法都成功的解决了我的问题,也谢谢给我提出建议的各位朋友,以后要自己多多写了,谢谢~~~
      

  8.   

    同样谢谢nc201(Bricklayer) 的新的思路。
      

  9.   

    try:class C{
      private String str;
      public C(String str){
        this.str = str;
      } 
      
      public int getValue(String s) throws NotInStrException{
        int index = str.indexOf(s + "=");
        if(index < 0) throw new NotInStrException("Out of mind");
        int index2 = str.indexOf("&",index);
        if(index2 < 0) index2 = str.length();
        return Integer.parseInt(str.substring(index + s.length() + 1,index2));
      }
    }class NotInStrException extends Exception{
       public NotInStrException(){}
       public NotInStrException(String msg){
         super(msg);
       }
    }
      

  10.   

    import java.util.HashMap;/**
     * <p>Title: </p>
     * <p>Description: </p>
     * <p>Copyright: Copyright (c) 2003</p>
     * <p>Company: </p>
     * @author unascribed
     * @version 1.0
     */public class MySplit {    private HashMap record = new HashMap();
        private MySplit() {
        }
        public static MySplit getInstance( String str ){
            MySplit ms = new MySplit();
            ms.init( str );
            return ms;
        }    public String getValue( String key ){
            return (String)record.get( key );
        }
        private void init( String s ){
            String[] xeqy = s.split("&");
            for(int i=0;i<xeqy.length;i++){
                String[] xy = xeqy[i].split("=");
                record.put( xy[ 0 ], xy[ 1 ] );
            }    }
        /**
         *
         */
        public static void main(String[] args) {
            MySplit ms = MySplit.getInstance( "a=3&b=4&c=5" );
            String a = ms.getValue("a");
            String b = ms.getValue("b");
            String c = ms.getValue("c");
            String d = ms.getValue("d");
            System.out.println("a = " + a);
            System.out.println("b = " + b);
            System.out.println("c = " + c);
            System.out.println("d = " + d);
        }
    }
      

  11.   

    esunboy(天使联盟)的有点漏洞。如aa=1&a=1这样就不对了。
    修改以下两句:
    this.str = "&" + str;
    String tmpStr = "&" + s + "=";
      

  12.   

    谢谢楼主指出我程序中的bug, 但楼主的写法(String tmpStr = "&" + s + "=";)也有问题, 如果是aa=1&a=1, 如果要getValue("aa")就不能查不出来了。
     Apollo47(阿波罗) 的写法不错, 建议楼主采用。
    其实正则表达式应该是速度应该是最快的。
      

  13.   

    http://expert.csdn.net/Expert/TopicView1.asp?id=3063668