Mohamed Nagy Mohamed Nagy - 1 year ago 108
Java Question

Convert array of Strings to String

Hello i have a little problem with array conversion

i want to convert this array format

To this String Format
with commas and parentheses.

btw this array is dynamic in size may be increased or decreased

this is my try

public static void main(String[] args) throws FileNotFoundException, IOException {
// TODO code application logic here
String [] arr = {"x","y","z"};
String s = "(";

for(int i = 0; i <arr.length;i++){
s +="'".concat(arr[i]).concat("'") + ',' ;
if(i == arr.length-1)
s.replace(',', ')');



this is my output ('x','y','z',
i want to replace the last comma with ')'

Answer Source

This also will do the magic..

Please use StringBuilder for these kind of string operations..

for (int i = 0; i < arr.length; i++) {
            s += "'".concat(arr[i]).concat("'") + ',';

        s = s.concat(")");

        System.out.print(s.replace(",)", ")"));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download