Following program will return the string by concatenating
given strings along with concatenation character or string.
Implementation:
package com.lenin;
public final class StringConcator
{
private static String EMPTY = "";
private
StringConcator(){}
public static String concat(char concat,
String... values) {
boolean space = false;
StringBuilder
sb = new StringBuilder();
for (String value :
values) {
if (value != null && !EMPTY.equals(value))
{
if (space) {
sb.append(concat);
}
sb.append(value);
space
= true;
}
}
return sb.toString();
}
public static String
concat(String concat, String... values) {
boolean space = false;
StringBuilder
sb = new StringBuilder();
for (String value :
values) {
if (value != null && !EMPTY.equals(value))
{
if (space) {
sb.append(concat);
}
sb.append(value);
space
= true;
}
}
return sb.toString();
}
}
Test:
import
static com.lenin.StringConcator.concat;
public class
StringConcatTest {
public static void main(String[]
args) {
System.out.println(concat(' ', "Leninkumar", "Koppoju"));
System.out.println(concat(", ", "Spring", "Summer", "Autumn", "Winter"));
}
}
Output:
Leninkumar Koppoju
Spring,
Summer, Autumn, Winter
No comments:
Post a Comment