classCodeaft{publicstaticvoidmain(Stringargs[]){Strings="Welcome to Codeaft!";System.out.println("———————————————————————————————————————————");System.out.println("Program to split the given string");System.out.println("———————————————————————————————————————————");Stringsarr[]=s.split(" ");System.out.println(sarr[0]+"\n"+sarr[1]+"\n"+sarr[2]);System.out.println("———————————————————————————————————————————");}}
Output
codeaft@codeaft:~$ javac Codeaft.java codeaft@codeaft:~$ java Codeaft
———————————————————————————————————————————
Program to split the given string
———————————————————————————————————————————
Welcome
to
Codeaft!
———————————————————————————————————————————
codeaft@codeaft:~$
Example 2
Codeaft.java
classCodeaft{publicstaticvoidmain(Stringargs[]){Strings="Welcome to Codeaft!";System.out.println("———————————————————————————————————————————");System.out.println("Program to split the given string");System.out.println("———————————————————————————————————————————");Stringsarr[]=s.split("to");for(Stringstr:sarr){System.out.println(str);}System.out.println("———————————————————————————————————————————");}}
Output
codeaft@codeaft:~$ javac Codeaft.java codeaft@codeaft:~$ java Codeaft
———————————————————————————————————————————
Program to split the given string
———————————————————————————————————————————
Welcome
Codeaft!
———————————————————————————————————————————
codeaft@codeaft:~$
Dear User, Thank you for visitng Codeaft. If you are interested in technical articles, latest technologies, and our journey further, please follow us on LinkedIn.