Header Ad

Sunday, April 1, 2012

Concatenation of String array to String

This program will help to concatenate String array to a simple string with a delimiter( if require).

public class StrArrtoStr {

public static void main(String[] args) {
String weeks_days[] = new String[7];
weeks_days[0] = "Sunday";
weeks_days[1] = "Monday";
weeks_days[2] = "Tuesday";
weeks_days[3] = "Wednesday";
weeks_days[4] = "Thursday";
weeks_days[5] = "Friday";
weeks_days[6] = "Saturday";
StringBuffer finalString = new StringBuffer();

if (weeks_days.length > 0) {
finalString.append(weeks_days[0]);
for (int i = 1; i < weeks_days.length; i++) {
finalString.append(",");
finalString.append(weeks_days[i]);
}
}

System.out.println("Concatenation of String array to String:::"
+ finalString.toString());

}

}

In the above example, we have String array of length 7 having items(weeks). We need to concatenate all these into a one string.

The above program will provide a final output which is as follows:
Concatenation of String array to String::: Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday

No comments: