字符串拆分基于java中的字符的操作

时间:2013-10-27 07:05:45

标签: java string string-concatenation

我有一个文本框,我需要在变量中输入。输入是数据库表的扩展列,数字被视为字符串。这是输入条件:

  1. 只有一个输入,如5028
  2. 输入用连字符( - )分隔,如5028-5090。
  3. 输入用逗号(,)分隔,如5028,5029。
  4. 包含在单个输入中分隔的个别,连字符和逗号的输入,如5029,1234-4567,9876。
  5. 我已经为只有一个输入形成查询的条件,输入用连字符分隔,输入用逗号分别单独进行..现在根据我的需要我必须写条件,我必须格式化查询输入,如5029,1234 -4567,9876但我没有得到确切的逻辑..

    这是我的代码..

    if (extension != "") {
        if (extension.contains(",")) {
            query = query.concat(" and (extension='");
            String extn[] = extension.split(",");
            for (int i = 0; i < extn.length; i++) {
                System.out.println(extn[i]);
                query = query.concat(extn[i]).concat("'").concat(" or extension='");
                System.out.println(query);
            }
            query = query.substring(0, query.length() - 15);
            System.out.println(query);
            query = query.concat(")");
            System.out.println(query);
        } else if (extension.contains("-")) {
            query = query.concat(" and cast(extension as signed) >=");
            String extn[] = extension.split("-");
            for (int i = 0; i < extn.length; i++) {
                System.out.println(extn[i]);
                query = query.concat(extn[i]).concat(" And cast(extension as signed) <=");
    
            }
            query = query.substring(0, query.length() - 33);
            System.out.println(query);
    
        } else {
            query = query.concat(" and extension='" + extension).concat("'");
            System.out.println(query);
        }
    }
    

    请大家帮帮我.. 提前谢谢。

1 个答案:

答案 0 :(得分:1)

进行以下更改:    1.用if (extension != "")

替换if (!extension.equals(""))
  1. 首先,if (extension.contains(","))之后的条件块String extn[] = extension.split(",");再次拆分' - '上的每个字符串extn []并执行其他处理
  2. 试试这个

    if (!extension.equals("")) {
    if (extension.contains(",")) {
        query = query.concat(" and (extension='");
        String extn[] = extension.split(",");
        for(int k=0; k <extn.length; k++){
        if(extn[k].contains("-")){
            String subExtn[] = extn[k].split("-");
            for (int i = 0; i < subExtn.length; i++) {
                //System.out.println(subExtn[i]);
                query = query.concat(subExtn[i]).concat("'").concat(" or extension='");
    
            }
    
        }else{
            //for (int i = 0; i < extn.length; i++) {
               // System.out.println(extn[i]);
                query = query.concat(extn[k]).concat("'").concat(" or extension='");
              //  System.out.println(query);
            //}
        }
        //System.out.println(query);
        }
        query = query.substring(0, query.length() - 15);
       // System.out.println(query);
        query = query.concat(")");
        System.out.println(query);
    } else if (extension.contains("-")) {
        query = query.concat(" and cast(extension as signed) >=");
        String extn[] = extension.split("-");
        for (int i = 0; i < extn.length; i++) {
    
            query = query.concat(extn[i]).concat(" And cast(extension as signed) <=");
    
        }
        query = query.substring(0, query.length() - 33);
        System.out.println(query);
    
    } else {
        query = query.concat(" and extension='" + extension).concat("'");
        System.out.println(query);
    }
    }