-2

What's the best way to convert a string array to a delimiter separated string.

eg:

String[] arr = {"apple","orange","banana","round"}; -------> apple-orange-banana-round?

3
  • 1
    apple-orange-banana-round is a String? Commented Nov 18, 2014 at 2:57
  • Create a StringBuffer, loop arr and append each word to the StringBuffer Commented Nov 18, 2014 at 2:58
  • see this link Commented Nov 18, 2014 at 2:58

1 Answer 1

0

You can use a StringBuffer or StringBuilder (StringBuffer is thread safe) to make this:

StringBuffer sb = new StringBuffer();
for(String s : arr){
    sb.append(s + "-");
}
sb.deleteCharAt(sb.length()-1);
return sb.toString();

Does this make sense to you?

Sign up to request clarification or add additional context in comments.

1 Comment

It will work and I did think of this solution. Is this the best solution of this (common) problem?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.