Search

Dark theme | Light theme
Showing posts with label JavaJoy:Strings. Show all posts
Showing posts with label JavaJoy:Strings. Show all posts

March 11, 2021

Java Joy: Formatting A String Value With Formatted Method

Java 15 introduced the multi-line string value referred to as a text block. With this introduction also the formatted method was added to the String class. The method can be invoked on a String value directly and function exactly as the static String.format method. The nice thing is that now we directly can use a method on the value instead of having to use a static method where the value is passed as argument.

In the following example we use the formatted method for a normal String value and a text block:

package mrhaki.string;

public class Formatted {
    public static void main(String[] args) {
        String text = "Hi, %s, thank you for reading %d blogs".formatted("mrhaki", 2);
        assert "Hi, mrhaki, thank you for reading 2 blogs".equals(text);

        String email = """
        Hello %s,

        thank you for reading %d blogs.
        """.formatted("Hubert", 2);

        assert """
                Hello Hubert,

                thank you for reading 2 blogs.
                """.equals(email);
    }
}

Written with Java 15.

March 3, 2021

Java Joy: Apply Function To String With Transform

In Java 12 the transform method was add to the String class. This method accepts a Function as argument. The function must have a single parameter of type String and can return any other type. The nice thing is that it works on a String instance, so we can directly use the transform method when we have a String value. We don't have to pass the String object to another method to tranform it, but we can define the tranformation function close to the String value.

In the following example we take a String value and apply some functions with the transform method:

package mrhaki.lang;

import java.util.stream.Collectors;

public class StringTransform {
    public static void main(String[] args) {
        String alphabet = "abcdefghijklmnopqrstuvwxyz";
        
        // Find all letters that have an even
        // int representation and join the results
        // back into a string.
        String letters = alphabet
                .transform(s -> s.chars()
                                 .filter(n -> n % 2 == 0)
                                 .mapToObj(n -> String.valueOf((char) n))
                                 .collect(Collectors.joining()));

        assert "bdfhjlnprtvxz".equals(letters);

        // Transform the string to a User object.
        User user = "mrhaki,Hubert Klein Ikkink"
                .transform(name -> new User(name.split(",")[0], name.split(",")[1]));

        assert "mrhaki".equals(user.alias);
        assert "Hubert Klein Ikkink".equals(user.fullName);
    }

    /**
     * Simple class to store alias and full name.
     */
    private static class User {
        private final String alias, fullName;

        private User(final String alias, final String fullName) {
            this.alias = alias;
            this.fullName = fullName;
        }
    }
}

Written with Java 15.