Skip to content

Instantly share code, notes, and snippets.

@nowshad-hasan
Last active October 10, 2021 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nowshad-hasan/530577ae5d0be3e0c5e076c66d84032a to your computer and use it in GitHub Desktop.
Save nowshad-hasan/530577ae5d0be3e0c5e076c66d84032a to your computer and use it in GitHub Desktop.
String input = "Hello Java 17!\nIt's a new LTS.";
public static void indentString(String input) {
// Positive value add spaces beginning of each line.
String positiveIndent5 = input.indent(5); // 5 spaces
/*
1. Hello Java 17!
It's a new LTS.
*/
// Negative value remove spaces from each line.
String negativeIndent2 = positiveIndent5.indent(-2); // 3 spaces
/*
1. Hello Java 17!
It's a new LTS.
*/
// For negative value, if the value is greater than the actual spaces the string actually has,
// it removes all the spaces.
String negativeIndent10 = positiveIndent5.indent(-10); // no space
/*
1.Hello Java 17!
It's a new LTS.
*/
}
// transform(java.util.function.Function function) takes a Function to transform a string.
public static void transformString(String input) {
input.transform(s -> s.trim())
.transform(s -> s.toUpperCase())
.transform(s -> s.lines())
.forEach(s -> System.out.println(s));
/*
HELLO JAVA 17!
IT'S A NEW LTS.
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment