1+ package com .baeldung .array .printarraywithoutbracketcomma ;
2+
3+ import static org .junit .jupiter .api .Assertions .assertEquals ;
4+
5+ import java .util .Arrays ;
6+ import java .util .stream .Collectors ;
7+ import java .util .stream .Stream ;
8+
9+ import org .apache .commons .lang3 .StringUtils ;
10+ import org .junit .jupiter .api .Test ;
11+
12+ import com .google .common .base .Joiner ;
13+
14+ public class PrintArrayWithoutBracketCommaTest {
15+
16+ String [] content = new String [] { "www." , "Baeldung." , "com" };
17+
18+ @ Test
19+ public void givenArray_whenUsingStringBuilder_thenPrintedArrayWithoutCommaBrackets () {
20+ StringBuilder builder = new StringBuilder ();
21+ for (String element : content ) {
22+ builder .append (element );
23+ }
24+
25+ assertEquals ("www.Baeldung.com" , builder .toString ());
26+ }
27+
28+ @ Test
29+ public void givenArray_whenUsingStringReplace_thenPrintedArrayWithoutCommaBrackets () {
30+ String result = Arrays .toString (content )
31+ .replace ("[" , "" )
32+ .replace ("]" , "" )
33+ .replace (", " , "" );
34+
35+ assertEquals ("www.Baeldung.com" , result );
36+ }
37+
38+ @ Test
39+ public void givenArray_whenUsingStringReplaceAll_thenPrintedArrayWithoutCommaBrackets () {
40+ String result = Arrays .toString (content ).replaceAll ("\\ [|\\ ]|, " , "" );
41+
42+ assertEquals ("www.Baeldung.com" , result );
43+ }
44+
45+ @ Test
46+ public void givenArray_whenUsingStringJoin_thenPrintedArrayWithoutCommaBrackets () {
47+ String result = String .join ("" , content );
48+
49+ assertEquals ("www.Baeldung.com" , result );
50+ }
51+
52+ @ Test
53+ public void givenArray_whenUsingStream_thenPrintedArrayWithoutCommaBrackets () {
54+ String result = Stream .of (content ).collect (Collectors .joining ("" ));
55+
56+ assertEquals ("www.Baeldung.com" , result );
57+ }
58+
59+ @ Test
60+ public void givenArray_whenUsingStringUtilsJoin_thenPrintedArrayWithoutCommaBrackets () {
61+ String result = StringUtils .join (content , "" );
62+
63+ assertEquals ("www.Baeldung.com" , result );
64+ }
65+
66+ @ Test
67+ public void givenArray_whenUsingJoinerJoin_thenPrintedArrayWithoutCommaBrackets () {
68+ String result = Joiner .on ("" ).join (content );
69+
70+ assertEquals ("www.Baeldung.com" , result );
71+ }
72+ }
0 commit comments