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

前端 未结 10 1228
夕颜
夕颜 2021-02-05 06:12

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., lis

相关标签:
10条回答
  • 2021-02-05 06:49

    Try this

        String[] str = {"url1","url2","url3","url4"};
        ArrayList<String> strList = new ArrayList<String>();
        for (String k:str)
            strList.add("'" +k+ "'");
        System.out.println("Printing the list");
        for(String k:strList)
            System.out.println(k);
    
    0 讨论(0)
  • 2021-02-05 06:54
    1. Iterate the list (for/while).

    2. For each element in the list append <element-of-list>. Hint: use append() on StringBuilder.

    3. Truncate/substring the list to remove the last element added. Hint: use substring on String class.

    0 讨论(0)
  • 2021-02-05 06:59

    for list you can convert this by Java 8

     list.stream().collect(Collectors.joining("','", "'", "'"));
    
    0 讨论(0)
  • 2021-02-05 06:59

    If you have your elements in an array, you can do something like this:

    import java.util.ArrayList;
    
    class Test1{
        public static void main(String[] args) {
            String[] s = {"url1","url2","url3","url4"};
    
            ArrayList<String> sl = new ArrayList<String>();
            for (int i = 0; i < s.length; i++) {
                sl.add("'" + s[i] + "'"); 
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-05 07:00
    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'
    
    0 讨论(0)
  • 2021-02-05 07:01

    Also you can use Java8 built-in features for String concatenation and third-party libraries for wrapping each string in quotes.

    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.mockito.runners.MockitoJUnitRunner;
    import org.springframework.util.StringUtils;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.StringJoiner;
    
    @RunWith(MockitoJUnitRunner.class)
    public class StringJoinerTest {
        @Test
        public void testStringJoiner() {
    
            //init test data
            String s1 = "team1";
            String s2 = "team2";
            List<String> teams = new ArrayList<>(2);
            teams.add(s1);
            teams.add(s2);
    
            //configure StringJoiner
            //when some values will be added to joiner it will return string started with prefix "(" an finished with suffix ")"
            //between prefix and suffix values will be separated with delimiter ", "
            StringJoiner teamNames = new StringJoiner(", ", "(", ")");
            //if nothing has been added to the joiner it will return  this value ('')
            teamNames.setEmptyValue("(\'\')");
    
            //fill joiner with data
            for (String currentString : teams) {
                //if you need to wrap each string in single quotes you can do this via org.apache.commons.lang3.StringUtils#wrap
                // or org.springframework.util.StringUtils#quote
                teamNames.add(StringUtils.quote(currentString));
            }
    
            System.out.println(teamNames.toString());
        }
    }
    
    0 讨论(0)
提交回复
热议问题