A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://howtodoinjava.com/java/string/java-string-concat-method-example/ below:

Java String concat() with Examples

The Java String.concat() concatenates the specified string to the end of the current string.

Internally, Java creates a new character array with a combined length of the current and argument string. Then it copies all content from both strings into this new array. Finally, the combined character array is converted to a new string and returned.

1. String.concat() API

The concat() API concatenates the specified string to the end of this string.

public String concat(String str);
2. String.concat() Example

The following Java program concatenates two strings to produce a combined string.

String str = "Hello";

Assertions.assertEquals("Hello World", str.concat(" World"));
3. Null and Empty Strings

Note that the method will return the original string if we can pass an empty string as the argument string.

Assertions.assertEquals("Hello", str.concat(""));

The null is not an accepted input and causes NullPointerException.

Assertions.assertThrows(NullPointerException.class, ()->{
    str.concat(null);
});

Happy Learning !!

References: Java String Doc

Source Code on Github


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4