I have the following piece of code that splits the string and returns an array of strings.
public static void main(String[] args) {
String name="what is going on";
String[] ary = name.split("");
System.out.println(Arrays.toString(ary));
}
//output: [, w, h, a, t, , i, s, , g, o, i, n, g, , o, n]
To prevent the trailing spaces, the following regex was employed during split. but I would like to know how it works
public static void main(String[] args) {
String name="what is going on";
String[] ary = name.split("(?!^)");
System.out.println(Arrays.toString(ary));
} //[w, h, a, t, , i, s, , g, o, i, n, g, , o, n]
if someone can explain what the regex looks for and how that regex is used for split, it will be very helpful for Java beginner community. Thanks a lot