0
String[] name = request.getParameterValues("name");

 name stored as ['anil,raju,anitha']

how to convert it to

            'anil','raju','anitha'

here my code

        List<String> wordList = Arrays.asList(name);  

  for (String e : wordList)  
  {  
     System.out.println(e);  
  }  

please help me to get expected output

1
  • Be very careful. User input (such as request parameters) needs to be validated before you use it in a query. Commented Nov 17, 2015 at 8:51

3 Answers 3

2

This has been quite a while ago, so here are some updated examples:

import java.util.stream.Collectors;
import java.util.stream.Stream;

public class JoiningTest {

    public static void main(String[] args) {
        String[] test = { "word1", "word2", "word3"};

        String join = String.join(",", test);
        System.out.println(join);

        String collect = Stream.of(test).collect(Collectors.joining(","));
        System.out.println(collect);
    }

}

Which will print:

word1,word2,word3
word1,word2,word3

Use apache commons lang:

public static void main(String[] args) {
        String[] test = {"1", "2", "3", "4"};
        String inClause = StringUtils.join(test, ",");
        System.out.println(inClause);
}

Prints:

1,2,3,4

Or the new StringJoiner (for your prefix/suffix):

StringJoiner joiner = new StringJoiner("','","'", "'");
        for(String s: test)
            joiner.add(s);

        System.out.println(joiner.toString());

Prints:

'1','2','3','4'
Sign up to request clarification or add additional context in comments.

Comments

1

You can use Stringbuilder like:

Iterator<String> itr = new ArrayList(Arrays.asList(names)).iterator();

    while(itr.hasNext()) {

    sb.append("'").append(itr.next()).append("'");

        if(itr.hasNext()) {
            sb.append(",");
        }
    }
System.out.println(sb.toString);  

1 Comment

You forgot the , :)
0
String[] name = {"anil","raju","anitha"};
String nameConcate = "" ;
for(int i = 0 ; i < name.length ; i++){
   nameConcate +="'"+name[i]+"',";
}
nameConcate=nameConcate.substring(0,nameConcate.length()-1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.