Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I have a list as below

[url1,url2,url3,url4]  

This list will be based on multiple selection from drop down list of HTML. So list size i.e., list elements change dynamically. My problem is I am unable to get the logic for getting single quotes to the strings. I want the above string list to be displayed as

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

That is single quotes (') for each string and commas (,) should not be eliminated. Please help me how to achieve this using Java.

like image 656
user2515189 Avatar asked Jul 01 '13 08:07

user2515189


People also ask

How do you add single quotes to a string in Java?

String str1 = "This is Jack's mobile"; String str2 = "\"This is it\"!"; Above, for single quote, we have to mention it normally like. However, for double quotes, use the following and add a slash in the beginning as well as at the end.

How do you add a comma separated string to a list in Java?

List<String> items = Arrays. asList(commaSeparated. split(",")); That should work for you.

Can you use single quotes for strings in Java?

A string literal is bracketed by either single quotes ( ' ) or double quotes ( " ). When single quotes bracket a string literal, the value of the literal is the value within the quotes. When double quotes are used, any references to variables or expressions within the quotes are interpolated.


1 Answers

for list you can convert this by Java 8

 list.stream().collect(Collectors.joining("','", "'", "'")); 
like image 162
jitendra varshney Avatar answered Sep 28 '22 19:09

jitendra varshney