Categories
5G Network
Agile
Amazon EC2
Android
Angular
Ansible
Arduino
Artificial Intelligence
Augmented Reality
AWS
Azure
Big Data
Blockchain
BootStrap
Cache Teachniques
Cassandra
Commercial Insurance
C#
C++
Cloud
CD
CI
Cyber Security
Data Handling
Data using R
Data Science
DBMS
Design-Pattern
DevOps
ECMAScript
Fortify
Ethical Hacking
Framework
GIT
GIT Slack
Gradle
Hadoop
HBase
HDFS
Hibernate
Hive
HTML
Image Processing
IOT
JavaScript
Java
Jenkins
Jira
JUnit
Kibana
Linux
Machine Learning
MangoDB
MVC
NGINX
Onsen UI
Oracle
PHP
Python
QTP
R Language
Regression Analysis
React JS
Robotic
Salesforce
SAP
Selenium
Service Discovery
Service Now
SOAP UI
Spark SQL
Testing
TOGAF
Research Method
Virtual Reality
Vue.js
Home
Recent Q&A
Feedback
Ask a Question
Java Strings New Methods
Home
>
JAVA
>
Java Strings New Methods
Jan 10, 2020
in
JAVA
Q:
Java Strings New Methods
1
Answer
0
votes
Jan 10, 2020
indent(int n)
This method adjusts the indentation of each line in the string based on the value of ‘n’ and also normalizes line termination characters.
If n > 0, then n spaces (U+0020) are inserted at the beginning of each line.
If n < 0, then up to n white space characters are removed from the beginning of each line. If a given line does not contain sufficient white space then all leading white space characters are removed. The tab character is also treated as a single character.
If n = 0, then the line remains unchanged. However, line terminators are still normalized.
String str = "*****\n Hi\n \tHello Pankaj\rHow are you?\n*****";
System.out.println(str.indent(0));
System.out.println(str.indent(3));
System.out.println(str.indent(-3));
Output:
*****
Hi
Hello Pankaj
How are you?
*****
*****
Hi
Hello Pankaj
How are you?
*****
*****
Hi
Hello Pankaj
How are you?
*****
Let’s look into the same examples through jshell.
➜ ~ jshell
| Welcome to JShell -- Version 12
| For an introduction type: /help intro
jshell> String str = "*****\n Hi\n \tHello Pankaj\rHow are you?\n*****";
str ==> "*****\n Hi\n \tHello Pankaj\rHow are you?\n*****"
jshell> str.indent(0)
$2 ==> "*****\n Hi\n \tHello Pankaj\nHow are you?\n*****\n"
jshell> str.indent(3)
$3 ==> " *****\n Hi\n \tHello Pankaj\n How are you?\n *****\n"
jshell> str.indent(-3)
$4 ==> "*****\nHi\nHello Pankaj\nHow are you?\n*****\n"
jshell>
Notice that \r is being normalized to \n when indent() method is called.
2. transform(Function<? super String,? extends R> f)
This method allows us to call a function on the given string. The function should expect a single String argument and produce an R result.
Let’s look at an example where we will use transform() method to convert a CSV string to the list of strings. Notice the use of lambda expressions to implement the functional interface.
String s = "Hi,Hello,Howdy";
List strList = s.transform(s1 -> {return Arrays.asList(s1.split(","));});
System.out.println(strList);
Output:
Java 12 String Methods
3. Optional<String> describeConstable()
Java 12 has introduced Constants API in JEP 334. If you look at the String class documentation, it implements two new interfaces from Constants API – Constable, and ConstantDesc. This method is declared in the Constable interface and implemented in the String class.
This method returns an Optional containing the nominal descriptor for this instance, which is the instance itself.
String so = "Hello";
Optional os = so.describeConstable();
System.out.println(os);
System.out.println(os.get());
Output:
Optional[Hello]
Hello
Hello
Java String Method DescribeConstable
Java String Method DescribeConstable
4. String resolveConstantDesc(MethodHandles.Lookup lookup)
This method is part of Constants API and declared in ConstantDesc interface. It resolves this instance as a ConstantDesc, the result of which is the instance itself.
jshell> import java.lang.invoke.MethodHandles;
jshell> String so1 = "Hello";
so1 ==> "Hello"
jshell> so1.resolveConstantDesc(MethodHandles.lookup());
$18 ==> "Hello"
Conclusion
The indent() and transform() methods are a great addition to the String class. The Constants API methods don’t have much usage for normal development related tasks.
Click here to read more about JAVA
Click here to read more about Insurance
Facebook
Twitter
LinkedIn
Related questions
0
votes
Q: Apart from the security aspect, what are the reasons behind making strings immutable in Java?
Dec 16, 2020
in
JAVA
#security-aspect
#strings
#java-strings
0
votes
Q: How to swap two strings in java?
Oct 22, 2020
in
JAVA
#swap-two-string
strings-swap-java
0
votes
Q: How to concatenate two strings in java?
Oct 21, 2020
in
JAVA
#concatenate-two-strings
strings-concatenate-in-java
0
votes
Q: Why are strings immutable in Java?
Oct 13, 2020
in
JAVA
#strings-immutable-in-java
#java-strings
0
votes
Q: How to compare two strings in java
Oct 9, 2020
in
JAVA
#compare-string-java
#java-string-compare
0
votes
Q: Which built-in method combines the text of two strings and returns a new string?
Oct 19, 2019
in
JavaScript
string-method
javascript-method
method-of-javascript
...