Java String.concat
Return a string with another string appended.
A simple example
String.concat
- Start with "a" and "b"
- Do this Join end to end
- Result "ab"
class Main {
public static void main(String[] args) throws Exception {
System.out.println("a".concat("b"));
}
}
ab
Syntax
public String concat(String str)
JDK 21 method signatures (1)
public String concat(String str)Run these examples in Java
Save one example at a time as Main.java. Use JDK 21.
java Main.javaBuild a locker identifier
A label printer receives an identifier in separate pieces instead of one finished string. Assemble the pieces in the intended order and check the separator where the two fields meet.
class Main {
public static void main(String[] args) throws Exception {
String wing = "East-";
System.out.println(wing.concat("17"));
}
}
East-17
The wing prefix comes before the locker number.
Add a receipt extension
An export routine receives the base name for a receipt. Add the extension to produce the printable filename and keep the original name available for any other export format.
class Main {
public static void main(String[] args) throws Exception {
String base = "march";
String filename = base.concat(".pdf");
System.out.println(base + " / " + filename);
}
}
march / march.pdf
The original base name remains available alongside the new filename.
Build a parcel shelf identifier
The shelf prefix already includes the hyphen. Combine the two fragments in order without adding another separator. What label goes to the printer?
What to watch for
Strings are immutable. Keep the returned value and use a builder for repeated appends in a large loop.