JAVA逗号将字符串分隔为单引号字符串

时间:2015-10-28 08:51:52

标签: java regex

我正在尝试从用户获取值并在我的SQL查询中使用这些值。 我可以在查询中附加单个值,但是当我在查询中使用IN时,我遇到了问题。

我从用户那里得到一个字符串:po1,po2,po3。 我希望将其转换为'po1','po2','po3',而前一代码中的更改次数最少。

我已经处理过为IN添加括号但面临添加单引号的问题。

这是我的整个代码:

public static void main(String[] args) {

    String poListString = "po1,po2,po3";
    String query = "SELECT shpUID.attribute_value                               shipunitid,         pal.container_id                                     palletid,         cas.container_id                                     caseid,         dl.delivery_line_id,        dg.delivery_group_id,        dlv.delivery_type_id,        dl.qty,        po.attribute_value                                   po,         Max(Isnull(comm.attribute_value, ''))                commodity,         Sum(Isnull(Cast(wgt.attribute_value AS FLOAT), 0.0)) weight,         Max(Isnull(wgtuom.attribute_value, ''))                weightuom,         Sum(Isnull(Cast(vol.attribute_value AS FLOAT), 0.0)) volume,       Max(Isnull(voluom.attribute_value, ''))                volumeuom,         GETDATE()                                         EventDate FROM   container pal         INNER JOIN container cas                 ON pal.container_id = cas.parent_container_id                    AND pal.container_type = 'Pallet'                    AND cas.container_type = 'Case'         INNER JOIN container_detail cdtl                 ON cdtl.container_id = cas.container_id         INNER JOIN container_delivery cd                 ON cd.detail_id = cdtl.detail_id         INNER JOIN delivery_line dl                 ON cd.delivery_line_id = dl.delivery_line_id         INNER JOIN delivery_group dg                 ON dg.delivery_group_id = dl.delivery_group_id        INNER JOIN delivery dlv                 ON dlv.delivery_num = dl.delivery_num         INNER JOIN delivery_type dt                 ON dt.delivery_type_id = dlv.delivery_type_id        LEFT OUTER JOIN attribute shpUID                      ON shpUID.attribute_id = cdtl.attribute_id                         AND shpUID.attribute_type = 'SHIP_UNIT_ID'         LEFT OUTER JOIN attribute wgt                      ON wgt.attribute_id = cdtl.attribute_id                         AND wgt.attribute_type = 'Weight'         LEFT OUTER JOIN attribute wgtuom                      ON wgtuom.attribute_id = cdtl.attribute_id                         AND wgtuom.attribute_type = 'Weightuom'        LEFT OUTER JOIN attribute vol                      ON vol.attribute_id = cdtl.attribute_id                         AND vol.attribute_type = 'Volume'                LEFT OUTER JOIN attribute voluom                      ON voluom.attribute_id = cdtl.attribute_id                         AND voluom.attribute_type = 'Volumeuom'         LEFT OUTER JOIN attribute comm                      ON comm.attribute_id = cdtl.attribute_id                         AND comm.attribute_type = 'COMMODITY'         LEFT OUTER JOIN attribute po                      ON po.attribute_id = cdtl.attribute_id                         AND po.attribute_type = 'PO'  WHERE           pal.container_id = @REPLACEMENT-1@  AND             dlv.delivery_num                  IN @REPLACEMENT-IN@  AND             dt.class = @REPLACEMENT-2@  GROUP  BY pal.container_id,            cas.container_id,            dl.delivery_line_id,            shpUID.attribute_value,            po.attribute_value,           pal.container_type,           dg.delivery_group_id,           dlv.delivery_type_id,           dl.qty UNION  SELECT shpUID.attribute_value                               shipunitid,         pal.container_id                                     palletid,         'UNKNOWN'                                            caseid,        dl.delivery_line_id,         dg.delivery_group_id,       dlv.delivery_type_id,      dl.qty,        po.attribute_value                                   po,         Max(Isnull(comm.attribute_value, ''))                commodity,         Sum(Isnull(Cast(wgt.attribute_value AS FLOAT), 0.0)) weight,         Max(Isnull(wgtuom.attribute_value, ''))                weightuom,         Sum(Isnull(Cast(vol.attribute_value AS FLOAT), 0.0)) volume,        Max(Isnull(voluom.attribute_value, ''))                volumeuom,         GETDATE()                                           EventDate FROM   container pal         INNER JOIN container_detail cdtl                 ON cdtl.container_id = pal.container_id                    AND pal.container_type = 'Pallet'         INNER JOIN container_delivery cd                 ON cd.detail_id = cdtl.detail_id         INNER JOIN delivery_line dl                 ON cd.delivery_line_id = dl.delivery_line_id         INNER JOIN delivery_group dg                   ON dg.delivery_group_id = dl.delivery_group_id        INNER JOIN delivery dlv                 ON dlv.delivery_num = dl.delivery_num         INNER JOIN delivery_type dt                 ON dt.delivery_type_id = dlv.delivery_type_id        LEFT OUTER JOIN attribute shpUID                      ON shpUID.attribute_id = cdtl.attribute_id                         AND shpUID.attribute_type = 'SHIP_UNIT_ID'         LEFT OUTER JOIN attribute wgt                      ON wgt.attribute_id = cdtl.attribute_id                         AND wgt.attribute_type = 'Weight'         LEFT OUTER JOIN attribute wgtuom                      ON wgtuom.attribute_id = cdtl.attribute_id                         AND wgtuom.attribute_type = 'Weightuom'        LEFT OUTER JOIN attribute vol                      ON vol.attribute_id = cdtl.attribute_id                         AND vol.attribute_type = 'Volume'           LEFT OUTER JOIN attribute voluom                      ON voluom.attribute_id = cdtl.attribute_id                         AND voluom.attribute_type = 'Volumeuom'         LEFT OUTER JOIN attribute comm                      ON comm.attribute_id = cdtl.attribute_id                         AND comm.attribute_type = 'COMMODITY'         LEFT OUTER JOIN attribute po                      ON po.attribute_id = cdtl.attribute_id                         AND po.attribute_type = 'PO'  WHERE           pal.container_id = @REPLACEMENT-1@  AND             dlv.delivery_num                  IN @REPLACEMENT-IN@  AND             dt.class = @REPLACEMENT-2@  GROUP  BY pal.container_id,            dl.delivery_line_id,            shpUID.attribute_value,            po.attribute_value,           pal.container_type,           dg.delivery_group_id,           dlv.delivery_type_id,           dl.qty";
    String value1 = "12345678910";
    String value2 = "INBOUND";

    String finalQuery = AppendQueryForIN.generateDynamicQueryForInAndValue(query, poListString, value1, value2);
    System.out.println(finalQuery);

}

public static String generateDynamicQueryForInAndValue(String sqlQuery, String stringList, String value1, String value2)
{
    StringBuilder listWithBrackets = new StringBuilder();
    listWithBrackets = listWithBrackets.append('(').append(stringList).append(')');
    String value1WithQoutes = "'"+value1+"'";
    String value2WithQoutes = "'"+value2+"'";
    String finalQuery = sqlQuery.replace("@REPLACEMENT-IN@", listWithBrackets);
    finalQuery = finalQuery.replace("@REPLACEMENT-1@", value1WithQoutes);
    finalQuery = finalQuery.replace("@REPLACEMENT-2@", value2WithQoutes);
    return finalQuery;
}

4 个答案:

答案 0 :(得分:2)

尝试使用String.split

String poListString = "po1,po2,po3";
String[] parts = poListString.split(",");
StringBuilder output = new StringBuilder();

for (String part : parts) {
    if (output.length() > 0) {
        output.append(",");
    }

    output.append("'").append(part).append("'");
}

System.out.println(output);

<强>输出:

'po1','po2','po3'

答案 1 :(得分:2)

假设字符串格式没有改变,您可以使用regex替换逗号以及字符串的开头和结尾

例如:

    String poListString = "po1,po2,po3";
    String result=poListString.replaceAll("^|$", "'").replaceAll(",", "','"); 
    //first replaceAll, adds ' to start and end and second replace change , to ','
    System.out.println(results);

<强>输出:

  

'PO1', 'PO2', 'PO3'

答案 2 :(得分:0)

仅使用Stream分享想法。

String quoted = Pattern.compile(",").splitAsStream(poListString)
                                    .collect(Collectors.joining("','", "'", "'"));

Pattern作为常量存储在类中并使用静态导入:

String quoted = PATTERN.splitAsStream(poListString).collect(joining("','", "'", "'"));

答案 3 :(得分:0)

public static String generateDynamicQueryForInAndValue(String sqlQuery, String stringList, String value1, String value2)
    {
        StringBuilder listWithBrackets = new StringBuilder();
        if(stringList == null || stringList.isEmpty()){
            stringList = "''";
        }else{

        stringList=stringList.replaceAll("^|$", "'").replaceAll(",", "','"); 
        }
        listWithBrackets = listWithBrackets.append('(').append(stringList).append(')');
        String value1WithQoutes = "'"+value1+"'";
        String value2WithQoutes = "'"+value2+"'";
         String finalQuery = sqlQuery.replace("@REPLACEMENT-IN@", listWithBrackets);
         finalQuery = finalQuery.replace("@REPLACEMENT-1@", value1WithQoutes);
         finalQuery = finalQuery.replace("@REPLACEMENT-2@", value2WithQoutes);
        return finalQuery;
    }