Convert String[] to comma separated string in java

后端 未结 23 2203
暖寄归人
暖寄归人 2020-11-29 22:40

i have one String[]

String[] name = {\"amit\", \"rahul\", \"surya\"};

i want to send name as parameter in sql query inside IN

23条回答
  •  旧巷少年郎
    2020-11-29 23:26

    here is a Utility method to split an array and put your custom delimiter, using

    String.replace(String,String)
    Arrays.toString(Object[])
    

    here it is :

    public static String toString(String delimiter, Object[]array){
        String s = "";
    
        // split array
        if (array != null && array.length > 0) {
            s = Arrays.toString(array).replace("[", "").replace("]", "");
        }
    
        // place delimiter (notice the space in ", ")
        if(delimiter != null){
            s = s.replace(", ", delimiter);
        }
    
        return s;
    }
    

    change the second argument type to suite your array type

提交回复
热议问题