Concatenate Strings with Suffix with Varargs in Java


Write a Java program to concatenate strings and append a suffix using varargs

The class contains a method called concatenateWithSuffix, which takes a string suffix and a variable number of strings as input. Inside this method, it iterates through each string in the input array and appends the specified suffix to each string using a StringBuilder.

The main method is the entry point of the program. Inside main, it calls the concatenateWithSuffix method with the suffix "_end" and several strings as arguments. It stores the concatenated result in the variable concatenated.

Finally, it prints out the result using System.out.println, displaying "Concatenated with Suffix : " followed by the concatenated string.

Source Code

public class SuffixStringConcatenator
{
	static String concatenateWithSuffix(String suffix, String... strings)
	{
		StringBuilder result = new StringBuilder();
		for (String str : strings)
		{
			result.append(str).append(suffix);
		}
		return result.toString();
	}
 
	public static void main(String[] args)
	{
		String concatenated = concatenateWithSuffix("_end", "Hello", "World", "Java");
		System.out.println("Concatenated with Suffix : " + concatenated);
	}
}

Output

Concatenated with Suffix : Hello_endWorld_endJava_end

Example Programs