Ryan
Ryan

Reputation: 1360

Method to concatenate 2 Strings in Java

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?

Upvotes: 12

Views: 30926

Answers (5)

Carl Manaster
Carl Manaster

Reputation: 40336

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;
    }

}

Upvotes: 0

Striker
Striker

Reputation: 329

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;  
}

Upvotes: 0

nishu
nishu

Reputation: 1493

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"

Upvotes: 4

Michael Borgwardt
Michael Borgwardt

Reputation: 346260

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;
}

Upvotes: 15

Jon Skeet
Jon Skeet

Reputation: 1499880

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.

Upvotes: 14

Related Questions