When splitting a string with the specified regular expression, use the split method.
split
split.java
import java.util.*;
public class Main {
    public static void main(String[] args) throws Exception {
        String str1 = "Lettuce pumpkin cucumber";
        String str2 = "Strawberry,Apple,Kiwi";
        
        //Whitespace delimited
        String[] split1 = str1.split(" "); 
        for ( int i = 0; i < split1.length; i++ ) { 
          System.out.println(split1[i]); 
        } 
        
        //Comma separated
        String[] split2 = str2.split(","); 
        for ( int i = 0; i < split2.length; i++ ) { 
          System.out.println(split2[i]); 
        } 
    }
}
result
lettuce
pumpkin
Cucumber
Strawberry
Apple
Kiwi
        Recommended Posts