How to add or insert ' (single quotes) for every string in a list in which strings are separated by commas using Java

for list you can convert this by Java 8

 list.stream().collect(Collectors.joining("','", "'", "'"));

import org.apache.commons.lang.StringUtils;
...
...
String arr[] = new String[4];
arr[0] = "my";
arr[1] = "name";
arr[2] = "is";
arr[3] = "baybora";

String join = "'" + StringUtils.join(arr,"','") + "'";

Result:

'my','name','is','baybora'

You can use Java 8 utility class java.util.StringJoiner.

This class specifically introduced to format list of string values.

For example:

StringJoiner joiner = new StringJoiner("','", "'", "'");
        joiner.add("url1");
        joiner.add("url2");
        joiner.add("url3");
        joiner.add("url4");
        System.out.println(joiner);

Output

'url1','url2','url3','url4'

Note that, in constructor of StringJoiner,

StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix)

we are passing single quote as prefix and suffix to satisfy your specific requirement.

Tags:

Java

String

List