Library contents

Java String.concat

Return a string with another string appended.

A simple example

String.concat

  1. Start with "a" and "b"
  2. Do this Join end to end
  3. Result "ab"
Both letters appear in the same order, with no separator added.
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        System.out.println("a".concat("b"));
    }
}
Output
ab

Syntax

Java 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.java

Build 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.

From the programString wing = "East-"
ResultEast-17
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        String wing = "East-";
        System.out.println(wing.concat("17"));
    }
}
Output
East-17

The wing prefix comes before the locker number.

Advertisements

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.

From the programString base = "march"
Resultmarch / march.pdf
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        String base = "march";
        String filename = base.concat(".pdf");
        System.out.println(base + " / " + filename);
    }
}
Output
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?

Given'North-' · '12'
Worked resultNorth-12

What to watch for

Strings are immutable. Keep the returned value and use a builder for repeated appends in a large loop.

Documentation for String.concat