Split() String Method Program in Java

This is a Java Program to Split into Pieces Wherever a Space is Found.

This is one of the important methods of String. split(String regex) :- Splits this string around matches of the given regular expression.

Here is the source code of the Java Program to Split into Pieces Wherever a Space is Found. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. public class Split_Demo
  2. {
  3.     public static void main(String... a)
  4.     {
  5.        String s1 = "HelloJava from JavaGuy";
  6.        String s2[] = s1.split(" ");
  7.        String part1 = s2[0];
  8.        String part2 = s2[1];
  9.        String part3 = s2[2];
  10.         System.out.println("The string after spliting is "+part1+ " and "+part2);
  11.     }
  12. }

Output:

$ javac Split_Demo.java
$ java Split_Demo
 
The string after spliting is Hello and Java

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.