Java 8: Introducing StringJoiner


import java.util.StringJoiner;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.Test;

public class StringJoinerTest {

    @Test
    public void join() {
        String expected = "duke,java";
        StringJoiner sj = new StringJoiner(",");
        sj.add("duke").add("java");
        String actual = sj.toString();
        assertThat(actual, is(expected));
    }

    @Test
    public void shortCut() {
        String expected = "duke,java";
        String actual = String.join(",", "duke", "java");
        assertThat(actual, is(expected));

    }
}

Thanks to @gschmidl for the idea:

See you at Java EE Workshops at MUC Airport or on demand and in a location very near you: airhacks.io!


Web Apps, SPA, PWA with vanilla Java Script (ES 6+), CSS 3 and WebStandards only. As simple as possible, but not simpler. See you at: (Progressive) Web apps, Single Page Apps and WebStandards airhacks workshops at MUC airport, Winter Edition

airhacks.fm the podcast:

Stay in touch: airhacks.news.

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
realworldpatterns.com
...the last 150 posts
...the last 10 comments
License