Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Java Dictionary

  1. Home
  2. Java Dictionary
  3. String.split() / String.join()

String.split() / String.join()

Methods for splitting a string into an array using a delimiter, and for joining an array or multiple strings with a delimiter. Widely used for parsing CSV data and generating formatted output.

Syntax

// Splits a string using a regular expression and returns a String array.
str.split(String regex);
str.split(String regex, int limit);

// Joins multiple strings with a delimiter (Java 8 and later).
String.join(CharSequence delimiter, CharSequence... elements);
String.join(CharSequence delimiter, Iterable<? extends CharSequence> elements);

// Formats a string using a format specifier.
String.format(String format, Object... args);

Method List

MethodDescription
split(String regex)Splits the string at positions matching the regular expression and returns a String[]. Trailing empty strings are removed.
split(String regex, int limit)Specifies the maximum number of substrings to return. A negative value preserves trailing empty strings.
String.join(delimiter, elements)Returns a new string by joining multiple strings with the specified delimiter.
String.format(format, args)Returns a string with arguments inserted according to the format specifier. Supports printf-style formatting.

Sample Code

// Split a comma-separated string with split().
String csv = "apple,banana,cherry";
String[] fruits = csv.split(",");
for (String fruit : fruits) {
    System.out.println(fruit); // Prints "apple", "banana", and "cherry" in order.
}

// Limit the number of resulting substrings.
String data = "a:b:c:d";
String[] parts = data.split(":", 2); // Split into at most 2 parts.
System.out.println(parts[0]); // Prints "a".
System.out.println(parts[1]); // Prints "b:c:d".

// Join an array with String.join().
String joined = String.join(", ", "Java", "Python", "Swift");
System.out.println(joined); // Prints "Java, Python, Swift".

// Join a List.
java.util.List<String> list = java.util.Arrays.asList("赤", "青", "緑");
System.out.println(String.join(" / ", list)); // Prints "赤 / 青 / 緑".

// Build a formatted string with String.format().
String msg = String.format("Name: %s, Age: %d", "Taro Yamada", 30);
System.out.println(msg); // Prints "Name: Taro Yamada, Age: 30".

// Display a decimal value with two decimal places.
double price = 1234.5;
System.out.println(String.format("Price: %.2f", price)); // Prints "Price: 1234.50".

Notes

split() treats its argument as a regular expression. If you use characters with special meaning in regex — such as a period (.) or pipe (|) — as delimiters, you must escape them: \\. or \\|.

String.join() is a static method added in Java 8 that makes it easy to join arrays and lists. For more complex string manipulation, using StringBuilder's append() is more efficient.

To replace parts of a string, use replace() / replaceAll(). To convert between a string and a character array, use toCharArray().

If you find any errors or copyright issues, please .