Mahesh Dasari
Mahesh Dasari

Reputation: 41

How to remove the null before resultant string after concatenation..?

How to remove the null after concatenation of two strings.

Ex:
String a = null;
String b = Hello;
a+=b;
System.out.println(a);// output is nullhello

here, i need output as only hello, Thanks in advance.

Upvotes: 1

Views: 1129

Answers (5)

Anton Balaniuc
Anton Balaniuc

Reputation: 11739

It might be a bit of overhead, but you can use Objects.toString method:

String a = null;
String b = Hello;
System.out.println(Objects.toString(a,"").concat(Objects.toString(b,"")));

public static String toString(Object o, String nullDefault)

Returns the result of calling toString on the first argument if the first argument is not null and returns the second argument otherwise.

This will handle all the possible cases {(a == null, b!= null),(a != null, b == null),(a == null, b == null),(a != null, b!= null)}

other alternative will be using Optional

System.out.println(
        Optional.ofNullable(a).orElse("")
                .concat(Optional.ofNullable(b).orElse(""))
);

public T orElse(T other)

Return the value if present, otherwise return other.

This will be identical to the first solution. Both of this solutions has one common disadvantage: code needs to be duplicated for every additional variable. For example if now we need to concatenate 3 variables a,b and c. We will have to just copy and paste same code for c.

In this case we can use Streams

System.out.println(
        Stream.of(a,b)
                .filter(Objects::nonNull)
                .collect(Collectors.joining())
); 

It will create a stream consisting of strings a and b; will filter out all strings which are null and finally collect all remaining strings using empty delimiter. In case if there are more then 2 variables we will just add them to initial stream.

I hope you will find this helpful.

Upvotes: 1

Oleg Cherednik
Oleg Cherednik

Reputation: 18245

You can use custom method for this, this is one line of code:

public static String concat(String a, String b) {
    return a == null ? b : b == null ? a : a + b;
}

Upvotes: 1

Bathsheba
Bathsheba

Reputation: 234665

The Java designers thought it a good idea to treat the compound assignment by sum operator += when applied to a null lvalue java.lang.String reference as a special case by injecting a (rather arbitrary) textual stringification of nullness.

(I see it as a manifest act of utter madness: an alternative would include throwing a NullPointerException as would happen if += is applied to a numeric boxed type such as java.lang.Integer.)

If you don't want this behaviour then you need to program specifically to obviate it.

Upvotes: 2

Anthony Raymond
Anthony Raymond

Reputation: 7862

You may use Google's Guava library to get rid of null before concatenation.

String a = Strings.nullToEmpty(null);
String b = Strings.nullToEmpty("Hello");
a+=b;
System.out.println(a);// output is hello

It makes the code more explicit about the fact that you don't want null, IMO it's more readable as than a ternary expression.

Upvotes: 0

Prince Odame
Prince Odame

Reputation: 544

You can use this

 String a = null;
 String b = "Hello";
 a = ((a==null) ? "": a) + b;
 System.out.println(a);

Upvotes: 4

Related Questions