12
votes

I have a method in Java that concatenates 2 Strings. It currently works correctly, but I think it can be written better.

public static String concat(String str1, String str2) {
  String rVal = null;
  if (str1 != null || str2 != null) {
    rVal = "";
    if (str1 != null) {
      rVal += str1;
    }
    if (str2 != null) {
      rVal += str2;
    }      
  }    
  return rVal;
}

Here are some of the requirements:

  1. If both str1 and str2 are null, the method returns null
  2. If either str1 or str2 is null, it will just return the not null String
  3. If str1 and str2 are not null, it will concatenate them
  4. It never adds "null" to the result

Can anyone do this with less code?

5
If I call str1.concat(str2), it will throw a NullPointerException when str1 is null. - Ryan
Couldn't you just use the StringBuilder? - Ant
@Ant I am not sure that handles the null cases very well. - Ryan
This question really has 7 votes? Really? Just to be able to re-write the same thing with a ternary operator? - GreenieMeanie

5 Answers

15
votes

Using only plain if clauses:

public static String concat(String str1, String str2) {
    if(str1==null) return str2;
    if(str2==null) return str1;
    return str1 + str2;
}

Or, if you have a deep and passionate love for parentheses:

public static String concat(String str1, String str2) {
    if(str1==null)
    { 
        return str2;
    }
    if(str2==null) 
    {
        return str1;
    }
    return str1 + str2;
}
14
votes

Sure:

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

Note that this takes care of the "both null" case in the first condition: if str1 is null, then you either want to return null (if str2 is null) or str2 (if str2 is not null) - both of which are handled by just returning str2.

4
votes
import org.apache.commons.lang.StringUtils;

StringUtils.join([str1, str2]);

Joins the elements of the provided array into a single String containing the provided list of elements.

No separator is added to the joined String. Null objects or empty strings within the array are represented by empty strings.

 StringUtils.join(null)            = null
 StringUtils.join([])              = ""
 StringUtils.join([null])          = ""
 StringUtils.join(["a", "b", "c"]) = "abc"
 StringUtils.join([null, "", "a"]) = "a"
0
votes

Everyone seems to have missed condition 1 where if both strings are null it returns null. The simplest version to read (IMO) then becomes:

public static String concat(String str1, String str2) {  
    if(str1==null && str2==null) return null;  
    if(str1==null) return str2;  
    if(str2==null) return str1;  
    return str1 + str2;  
}
0
votes
public class ConcatTest extends TestCase {

    // 1. If both str1 and str2 are null, the method returns null
    public void testBothNull() throws Exception {
        assertNull(concat(null, null));
    }

    // 2. If either str1 or str2 is null, it will just return the not null
    // String
    public void testOneNull() throws Exception {
        assertEquals("a", concat(null, "a"));
        assertEquals("b", concat("b", null));
    }

    // 3. If str1 and str2 are not null, it will concatenate them
    public void testNonNull() throws Exception {
        assertEquals("ab", concat("a", "b"));
    }

    // 4. It never adds "null" to the result (not really testable)

    public static String concat(String a, String b) {
        if (a == null && b == null)
            return null;
        return denulled(a) + denulled(b);
    }

    private static String denulled(String s) {
        return s == null ? "" : s;
    }

}