BAEL-2962 Java Multi-line String (#7166)
* added readme * hexagonal architecture implemented * Java Multiline String
This commit is contained in:
parent
7c4019e384
commit
d67b026d29
@ -0,0 +1,67 @@
|
|||||||
|
package com.baeldung.string.multiline;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import java.io.StringWriter;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
|
import com.google.common.base.Joiner;
|
||||||
|
import com.google.common.collect.ImmutableList;
|
||||||
|
|
||||||
|
public class MultiLineString {
|
||||||
|
|
||||||
|
String newLine = System.getProperty("line.separator");
|
||||||
|
|
||||||
|
public String stringConcatenation() {
|
||||||
|
return "Get busy living"
|
||||||
|
.concat(newLine)
|
||||||
|
.concat("or")
|
||||||
|
.concat(newLine)
|
||||||
|
.concat("get busy dying.")
|
||||||
|
.concat(newLine)
|
||||||
|
.concat("--Stephen King");
|
||||||
|
}
|
||||||
|
|
||||||
|
public String stringJoin() {
|
||||||
|
return String.join(newLine,
|
||||||
|
"Get busy living",
|
||||||
|
"or",
|
||||||
|
"get busy dying.",
|
||||||
|
"--Stephen King");
|
||||||
|
}
|
||||||
|
|
||||||
|
public String stringBuilder() {
|
||||||
|
return new StringBuilder()
|
||||||
|
.append("Get busy living")
|
||||||
|
.append(newLine)
|
||||||
|
.append("or")
|
||||||
|
.append(newLine)
|
||||||
|
.append("get busy dying.")
|
||||||
|
.append(newLine)
|
||||||
|
.append("--Stephen King")
|
||||||
|
.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String stringWriter() {
|
||||||
|
StringWriter stringWriter = new StringWriter();
|
||||||
|
PrintWriter printWriter = new PrintWriter(stringWriter);
|
||||||
|
printWriter.println("Get busy living");
|
||||||
|
printWriter.println("or");
|
||||||
|
printWriter.println("get busy dying.");
|
||||||
|
printWriter.println("--Stephen King");
|
||||||
|
return stringWriter.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String guavaJoiner() {
|
||||||
|
return Joiner.on(newLine).join(ImmutableList.of("Get busy living",
|
||||||
|
"or",
|
||||||
|
"get busy dying.",
|
||||||
|
"--Stephen King"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public String loadFromFile() throws IOException {
|
||||||
|
return new String(Files.readAllBytes(Paths.get("src/main/resources/stephenking.txt")));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
4
java-strings-2/src/main/resources/stephenking.txt
Normal file
4
java-strings-2/src/main/resources/stephenking.txt
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
Get busy living
|
||||||
|
or
|
||||||
|
get busy dying.
|
||||||
|
--Stephen King
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.baeldung.string;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import com.baeldung.string.multiline.MultiLineString;
|
||||||
|
|
||||||
|
public class MultiLineStringUnitTest {
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenCompareMultiLineStrings_thenTheyAreAllTheSame() throws IOException {
|
||||||
|
MultiLineString ms = new MultiLineString();
|
||||||
|
assertEquals(ms.stringConcatenation(), ms.stringJoin());
|
||||||
|
assertEquals(ms.stringJoin(), ms.stringBuilder());
|
||||||
|
assertEquals(ms.stringBuilder(), ms.guavaJoiner());
|
||||||
|
assertEquals(ms.guavaJoiner(), ms.loadFromFile());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user