Java 12 Tutorial
Selected Reading
- Java 12 - Discussion
- Java 12 - Useful Resources
- Java 12 - Quick Guide
- Java 11 Tutorial
- Java 10 Tutorial
- Java 9 Tutorial
- Java 8 Tutorial
- Java Tutorial
- Java 12 - Microbenchmark
- Garbage Collection Enhancements
- Java 12 - String methods
- Java 12 - Teeing Collectors
- Java 12 - Compact Number Formatting
- Java 12 - File mismatch method
- Java 12 - Switch Expressions
- Java 12 - Environment Setup
- Java 12 - Overview
- Java 12 - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Java 12 - String methods
Java 12 - String methods
Java 12 introduces following new methods to String for easy formatting.
indent(n) method
Adjust the indention of each pne of string based on argument passed.
Usage
string.indent(n)
n > 0 - insert space at the begining of each pne.
n < 0 - remove space at the begining of each pne.
n < 0 and n < available spaces - remove all leading space of each pne.
n = 0 - no change.
transform(Function<? super String,? extends R> f) method
Transforms a string to give result as R.
Usage
String transformed = text.transform(value -> new StringBuilder(value).reverse().toString());
Optional<String> describeConstable() method
Returns Optional Object containing description of String instance.
Usage
Optional<String> optional = message.describeConstable();
resolveConstantDesc(MethodHandles.Lookup lookup) method
Returns descriptor instance string of given string.
Usage
String constantDesc = message.resolveConstantDesc(MethodHandles.lookup());
Consider the following example −
ApiTester.java
import java.lang.invoke.MethodHandles; import java.util.Optional; pubpc class APITester { pubpc static void main(String[] args) { String str = "Welcome to 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); } }
Output
Welcome to Tutorialspoint! Welcome to Tutorialspoint! avaJ Optional[Java] JavaAdvertisements