To divide a
String
into substrings in Java, use the split
method.Here's how you do it:
String string = "Hello World";
String[] dividedIntoWords = string.split("\\s");
String[] dividedIntoCharacters = string.split("");
String[] dividedByO = string.split("o");
// [Hello, World]
System.out.println(java.util.Arrays.toString(dividedIntoWords));
// [H, e, l, l, o, , W, o, r, l, d]
System.out.println(java.util.Arrays.toString(dividedIntoCharacters));
// [Hell, W, rld]
System.out.println(java.util.Arrays.toString(dividedByO));