2

this should be an easy one. I need to format a string array if the length is greater than 1 to look as following: "('STRING','STRING')

Thanx

if  (form.getSorCodes().length > 1)

            for( int i=0;i<form.getSorCodes().length;i++)
             {

                 //format here         

             }
1
  • Use StringBuffer to build up the string. One word of advice (given that I have no idea what sort of system this snippet is from) in theory getSorCodes() could be a time consuming operation, or the results may change between the 2 calls, so you are better off storing it's return value rather than calling it again. Commented Feb 7, 2012 at 3:56

3 Answers 3

5
String[] sorCodes = form.getSorCodes();
if  (sorCodes.length > 1) {
    StringBuilder builder = new StringBuilder("(");
    for( int i=0;i<sorCodes.length;i++) { 
       builder.append("'").append(sorCodes[i]).append("'");
       if(i < sorCodes.length - 1) {
          builder.append(",");
       }
    }
    builder.append(")");

    builder.toString(); //('STRING','STRING') 
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanx mate..bout to give it a try
0

Using Guava:

StringBuilder result = new StringBuilder("(");
Joiner.on(',').appendTo(result, stringArray);
return result.append(",").toString();

Comments

0

Just Use this Class:

StringBuilder result = new StringBuilder();

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.