Java 8 processing character stitching has NULL problem

tags: Java back end  java

We must deal with character stitching during the development process. If the stitching characters are empty, NULL will appear, so what should we do. Let's use the Java8 attribute to deal with it.

		String a = "Shanghai";
        String b =null;
                 String C = "Huaihai Road";
        String value = MessageFormat.format("{0}{1}{2}",Optional.ofNullable(a).orElse("")
                ,Optional.ofNullable(b).orElse("")
                ,Optional.ofNullable(c).orElse(""));

        System.out.println("---->"+a+b+c);
        System.out.println("****"+value);

It can be seen that optional.ofnullable (xx) .orelse (""); When the xx parameter is empty, we can use "" by default afterwards, and it can also be replaced with other default values.

Intelligent Recommendation

Remove the null character in Java

Three ways to remove empty characters in java 1.trim(): Remove the character spaces at the left and right ends of the string 2.repalaceAll(" "," "): remove all spaces in the string...

Java 8 Optional solves the problem of null pointer exception

When writing a program, it is often necessary to judge the null value of the created object or attribute, but sometimes it may be negligent to fail to judge the null, which will cause the null pointer...

Vue URL has a parameter stitching in the previous problem

The problem is found that the callback address returned when WeChat authorization is done. When I come back, I find that code fights in front of# Solution 1: After using new url (), get the following ...

– Java processing null

– Start When processing a collection, if there is a null element, it will result in a null pointer exception. – See more:Java functional programming – Description: Reprint, please in...

The difference between java null and "" null character

NULL: An empty object is declared, not a string, and can be assigned to any object. "": Represents an object instance declared, and the value of this object instance is an empty string with ...

More Recommendation

String stitching new pose - Java 8 StringJoiner

Foreword Stitching strings in daily work is definitely an indispensable operation. I just used it in my recent work. Then I remembered a new class of Java 8 that I saw a few days ago: the StringJoiner...

New pose for string stitching in Java 8: StringJoiner

inWhy Alibaba does not recommend using "+" in the for loop for string concatenationIn this article, we introduced several ways of string concatenation in Java, as well as its advantages and ...

Oracle character null processing, a little dizzy

1. The description of NULL in Oracle null Absence of a value in a column of a row. Nulls indicate missing, unknown, or inapplicable data.  NULL is a unique data type in the database. When a recor...

The problem of the physical assignment processing NULL

The project development involves a request entity. If it is empty, it is set to "", so that the GRPC server will obtain not much, and it will not be used as a processing condition. code show...

About C ++ String string stitching problem (and "character turnt string" problem)

(I only went to my lungs, I might abolish some time to write blogs (mainly writing some breath), but now there is almost that I can't do anything. text Don't ask me, "Why don't you directly turn ...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top