Java 12 introduces following new methods to String for easy formatting.
Adjust the indention of each line of string based on argument passed.
string.indent(n)
n > 0 - insert space at the begining of each line.
n < 0 - remove space at the begining of each line.
n < 0 and n < available spaces - remove all leading space of each line.
n = 0 - no change.
Transforms a string to give result as R.
String transformed = text.transform(value -> new StringBuilder(value).reverse().toString());
Returns Optional Object containing description of String instance.
Optional<String> optional = message.describeConstable();
Returns descriptor instance string of given string.
String constantDesc = message.resolveConstantDesc(MethodHandles.lookup());
Consider the following example −
ApiTester.java
import java.lang.invoke.MethodHandles; import java.util.Optional; public class APITester { public static void main(String[] args) { String str = "Welcome \nto Tutorialspoint!"; System.out.println(str.indent(0)); System.out.println(str.indent(3)); String text = "Java"; String transformed = text.transform(value -> new StringBuilder(value).reverse().toString()); System.out.println(transformed); Optional<String> optional = text.describeConstable(); System.out.println(optional); String cDescription = text.resolveConstantDesc(MethodHandles.lookup()); System.out.println(cDescription); } }
Welcome to Tutorialspoint! Welcome to Tutorialspoint! avaJ Optional[Java] Java