Close

Java 12 - String Changes

[Updated: Mar 24, 2019, Created: Mar 23, 2019]

Java 12, added various methods in java.lang.String class. Let's see each one of them with examples.

Indenting Strings

public String indent(int n)

This method adjusts the indentation of each line of this string.

If n > 0, this method inserts 'n' number of space characters (U+00200):

public class IndentExample {
  public static void main(String[] args) {
      String str = "a test string";
      System.out.println(str);
      System.out.println(str.length());
      System.out.println("-- indented string --");
      String indentedStr = str.indent(5);
      System.out.println(indentedStr);
      System.out.println(indentedStr.length());
  }
}
a test string
13
-- indented string --
a test string

19

Before inserting spaces, the input string is separated into lines. Each line is inserted with 'n' spaces, then suffixed with a line feed "\n" (U+000A). The resulting lines are then concatenated and returned.

Let's quote the resultant string in above example, to confirm that \n is appended at the end:

public class IndentExample2 {
  public static void main(String[] args) {
      String str = "a test string";
      System.out.println(str);
      String indentedStr = str.indent(5);
      System.out.println(indentedStr.endsWith("\n"));
      System.out.printf("'%s'%n", indentedStr);
      System.out.println("-- indented string with quotes --");
  }
}
a test string
true
' a test string
'
-- indented string with quotes --

Multiline string:

public class IndentExample3 {
  public static void main(String[] args) {
      String str = "a test\nstring";
      System.out.println(str);
      System.out.println("-- indented string --");
      String indentedStr = str.indent(5);
      System.out.println(indentedStr);
  }
}
a test
string
-- indented string --
a test
string

If 'n' is negative then n number of strings are removed:

public class IndentExample4 {
  public static void main(String[] args) {
      String str = "     a test\n     string";
      System.out.println(str);
      String indentedStr = str.indent(-2);
      System.out.println("-- negatively indented string --");
      System.out.println(indentedStr);
  }
}
     a test
string
-- negatively indented string --
a test
string

For n < 0, if a given line does not contain sufficient white space then all leading white space characters are removed.

public class IndentExample5 {
  public static void main(String[] args) {
      String str = "  a\n    test\n        string";
      System.out.println(str);
      String indentedStr = str.indent(-6);
      System.out.println("-- negatively indented string --");
      System.out.println(indentedStr);
  }
}
  a
test
string
-- negatively indented string --
a
test
string

If n = 0 then the line remains unchanged. However, line terminators are still added.

public class IndentExample7 {
  public static void main(String[] args) {
      String str = " a test string";
      System.out.println(str);
      String indentedStr = str.indent(0);
      System.out.println("-- negatively indented string --");
      System.out.printf("'%s'%n", indentedStr);
  }
}
 a test string
-- negatively indented string --
' a test string
'

Transforming Strings

 public <R> R transform(Function<? super String, ? extends R> f) 

This method allows us to apply a lambda function to string.

public class TransformExample {
  public static void main(String[] args) {
      String str = "1000";
      Integer integer = str.transform(Integer::parseInt);
      System.out.println(integer);
  }
}
1000

Method related to JVM Constants API (JEP 334)

Optional<? extends ConstantDesc> describeConstable()
Object resolveConstantDesc(MethodHandles.Lookup lookup) throws ReflectiveOperationException;

Java 12 also introduced JVM Constants API (JEP 334) which models nominal descriptions of key class-file and run-time artifacts, in particular constants that are loadable from the constant pool. This topic will be covered in future tutorials.

Example Project

Dependencies and Technologies Used:

  • JDK 12
Java 12 new methods in String Select All Download
  • java-12-string-changes
    • src
      • com
        • logicbig
          • example
            • IndentExample.java

    See Also