Skip to main content
added 9 characters in body
Source Link
Daniel De León
  • 13.8k
  • 5
  • 93
  • 74

A fun way to do it with pure JDK, in one duty line:

String[] array = new String[] { "Bill", "Bob", "Steve","[Bill]","1,2,3","Apple ][" };
String join = " and ";
    
String joined = Arrays.toString(array).replaceAll(", ", join)
        .replaceAll("(^\\[)|(\\]$)", "");
    
System.out.println(joined);

Output:

Bill and Bob and Steve and [Bill] and 1,2,3 and Apple ][


A not too perfect, & not too fun way!

String[] array = new String[] { "7, 7, 7","Bill", "Bob", "Steve", "[Bill]",
        "1,2,3", "Apple ][" };
String join = " and ";

for (int i = 0; i < array.length; i++) array[i] = array[i].replaceAll(", ", "~,~");
String joined = Arrays.toString(array).replaceAll(", ", join)
        .replaceAll("(^\\[)|(\\]$)", "").replaceAll("~,~", ", ");

System.out.println(joined);

Output:

7, 7, 7 and Bill and Bob and Steve and [Bill] and 1,2,3 and Apple ][

A fun way to do it with pure JDK, in one duty line:

String[] array = new String[] { "Bill", "Bob", "Steve","[Bill]","1,2,3","Apple ][" };
String join = " and ";
    
String joined = Arrays.toString(array).replaceAll(", ", join)
        .replaceAll("(^\\[)|(\\]$)", "");
    
System.out.println(joined);

Output:

Bill and Bob and Steve and [Bill] and 1,2,3 and Apple ][


A perfect, not too fun way!

String[] array = new String[] { "7, 7, 7","Bill", "Bob", "Steve", "[Bill]",
        "1,2,3", "Apple ][" };
String join = " and ";

for (int i = 0; i < array.length; i++) array[i] = array[i].replaceAll(", ", "~,~");
String joined = Arrays.toString(array).replaceAll(", ", join)
        .replaceAll("(^\\[)|(\\]$)", "").replaceAll("~,~", ", ");

System.out.println(joined);

Output:

7, 7, 7 and Bill and Bob and Steve and [Bill] and 1,2,3 and Apple ][

A fun way to do it with pure JDK, in one duty line:

String[] array = new String[] { "Bill", "Bob", "Steve","[Bill]","1,2,3","Apple ][" };
String join = " and ";
    
String joined = Arrays.toString(array).replaceAll(", ", join)
        .replaceAll("(^\\[)|(\\]$)", "");
    
System.out.println(joined);

Output:

Bill and Bob and Steve and [Bill] and 1,2,3 and Apple ][


A not too perfect & not too fun way!

String[] array = new String[] { "7, 7, 7","Bill", "Bob", "Steve", "[Bill]",
        "1,2,3", "Apple ][" };
String join = " and ";

for (int i = 0; i < array.length; i++) array[i] = array[i].replaceAll(", ", "~,~");
String joined = Arrays.toString(array).replaceAll(", ", join)
        .replaceAll("(^\\[)|(\\]$)", "").replaceAll("~,~", ", ");

System.out.println(joined);

Output:

7, 7, 7 and Bill and Bob and Steve and [Bill] and 1,2,3 and Apple ][

added 507 characters in body
Source Link
Daniel De León
  • 13.8k
  • 5
  • 93
  • 74

A fun way to do it with pure JDK, in one duty line:

String[] array = new String[] { "Bill", "Bob", "Steve","[Bill]","1,2,3","Apple ][" };
String join = " and ";
    
String joined = Arrays.toString(array).replaceAll(", ", join)
        .replaceAll("(^\\[)|(\\]$)", "");
    
System.out.println(joined);

Output:

Bill and Bob and Steve and [Bill] and 1,2,3 and Apple ][


A perfect, not too fun way!

String[] array = new String[] { "7, 7, 7","Bill", "Bob", "Steve", "[Bill]",
        "1,2,3", "Apple ][" };
String join = " and ";

for (int i = 0; i < array.length; i++) array[i] = array[i].replaceAll(", ", "~,~");
String joined = Arrays.toString(array).replaceAll(", ", join)
        .replaceAll("(^\\[)|(\\]$)", "").replaceAll("~,~", ", ");

System.out.println(joined);

Output:

7, 7, 7 and Bill and Bob and Steve and [Bill] and 1,2,3 and Apple ][

A fun way to do it with pure JDK, in one duty line:

String[] array = new String[] { "Bill", "Bob", "Steve","[Bill]","1,2,3","Apple ][" };
String join = " and ";
    
String joined = Arrays.toString(array).replaceAll(", ", join)
        .replaceAll("(^\\[)|(\\]$)", "");
    
System.out.println(joined);

Output:

Bill and Bob and Steve and [Bill] and 1,2,3 and Apple ][

A fun way to do it with pure JDK, in one duty line:

String[] array = new String[] { "Bill", "Bob", "Steve","[Bill]","1,2,3","Apple ][" };
String join = " and ";
    
String joined = Arrays.toString(array).replaceAll(", ", join)
        .replaceAll("(^\\[)|(\\]$)", "");
    
System.out.println(joined);

Output:

Bill and Bob and Steve and [Bill] and 1,2,3 and Apple ][


A perfect, not too fun way!

String[] array = new String[] { "7, 7, 7","Bill", "Bob", "Steve", "[Bill]",
        "1,2,3", "Apple ][" };
String join = " and ";

for (int i = 0; i < array.length; i++) array[i] = array[i].replaceAll(", ", "~,~");
String joined = Arrays.toString(array).replaceAll(", ", join)
        .replaceAll("(^\\[)|(\\]$)", "").replaceAll("~,~", ", ");

System.out.println(joined);

Output:

7, 7, 7 and Bill and Bob and Steve and [Bill] and 1,2,3 and Apple ][

added 28 characters in body
Source Link
Daniel De León
  • 13.8k
  • 5
  • 93
  • 74

A fun way to do it with pure JDK, in one duty line:

    String[] array = new String[] { "Bill", "Bob", "Steve","[Bill]","1,2,3","Apple ][" };
    String join = " and ";
    
    String joined = Arrays.toString(array).replaceAll(", ", join)
            .replaceAll("[\\[\\]]""(^\\[)|(\\]$)", "");
    
    System.out.println(joined);

Output:

Bill and Bob and Steve and [Bill] and 1,2,3 and Apple ][

A fun way to do it with pure JDK, in one duty line:

    String[] array = new String[] { "Bill", "Bob", "Steve" };
    String join = " and ";
    
    String joined = Arrays.toString(array).replaceAll(", ", join)
            .replaceAll("[\\[\\]]", "");
    
    System.out.println(joined);

Output:

Bill and Bob and Steve

A fun way to do it with pure JDK, in one duty line:

String[] array = new String[] { "Bill", "Bob", "Steve","[Bill]","1,2,3","Apple ][" };
String join = " and ";
    
String joined = Arrays.toString(array).replaceAll(", ", join)
        .replaceAll("(^\\[)|(\\]$)", "");
    
System.out.println(joined);

Output:

Bill and Bob and Steve and [Bill] and 1,2,3 and Apple ][

Source Link
Daniel De León
  • 13.8k
  • 5
  • 93
  • 74
Loading