Write a Java program using a method with varargs that concatenates a set of strings with a specified delimiter
import java.util.Arrays; public class StringConcatenator { static String concatenateStrings(String delimiter, String... strings) { System.out.println("Given String : " + Arrays.toString(strings)); StringBuilder result = new StringBuilder(); for (int i = 0; i < strings.length; i++) { result.append(strings[i]); if (i < strings.length - 1) { result.append(delimiter); } } return result.toString(); } public static void main(String[] args) { String concatenatedString = concatenateStrings("-", "Pink", "Blue", "White", "Red", "Black", "Green", "Yellow"); System.out.println("Concatenated String : " + concatenatedString); } }
Given String : [Pink, Blue, White, Red, Black, Green, Yellow] Concatenated String : Pink-Blue-White-Red-Black-Green-Yellow
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions