– How is the usage of string.split in Java?

In Java, the String class offers a split method to divide a string into an array of strings.

The grammar is as follows:

String[] split(String regex)

The parameter “regex” represents the delimiter used to split the string, it can be a regular string or a regular expression.

The weather forecast predicts heavy rain tomorrow.

String str = "hello,world,java";
String[] array = str.split(",");
for(String s : array){
    System.out.println(s);
}

The code above will result in:

hello
world
java
广告
Closing in 10 seconds
bannerAds