split() method to split a string into multiple strings using a delimiter.
split() method returns an array of String.
public class Main { public static void main(String[] args) { String str = "AL,FL,NY,CA,GA"; // Split str using a comma as the delimiter String[] parts = str.split(","); // Print the the string and its parts System.out.println(str); for (String part : parts) { System.out.println(part); } } }
join() method to the String class that joins multiple strings into one string. It is overloaded.
String join(CharSequence delimiter, CharSequence... elements)
String join(CharSequence delimiter, Iterable<? extends CharSequence> elements)
The following code uses the first version to join some strings:
public class Main { public static void main(String[] args) { // Join some strings using a comma as the delimiter String str = String.join(",", "AL", "FL", "NY", "CA", "GA"); System.out.println(str);//from www . j ava 2 s.co m } }