Constructing String with Symbol

#1 Example Program

package com.dineshkrish.string;
public class ConstructSymbolStr {
public static String construct(String[] values, char symbol) {
StringBuilder sb = new StringBuilder();
if (values != null && values.length > 0) {
for (int i = 0; i < values.length; i++) {
if (i != (values.length - 1)) {
sb.append(values[i] + symbol);
} else {
sb.append(values[i]);
}
}
}
return sb.toString();
}
public static void main(String[] args) {
String[] values = { "D", "I", "N", "E", "S", "H", "K", "R", "I", "S",
"H", "N", "A", "N" };
String constructedStr1 = construct(values, '|');
String constructedStr2 = construct(values, '-');
String constructedStr3 = construct(values, '*');
String constructedStr4 = construct(values, '_');
String constructedStr5 = construct(values, '.');
System.out.println(constructedStr1);
System.out.println(constructedStr2);
System.out.println(constructedStr3);
System.out.println(constructedStr4);
System.out.println(constructedStr5);
}
}

Output

D|I|N|E|S|H|K|R|I|S|H|N|A|N
D-I-N-E-S-H-K-R-I-S-H-N-A-N
D*I*N*E*S*H*K*R*I*S*H*N*A*N
D_I_N_E_S_H_K_R_I_S_H_N_A_N
D.I.N.E.S.H.K.R.I.S.H.N.A.N

References

1. Java String API

Tags:

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *