Java Program to Split into Pieces Wherever a Space is Found

«
»
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.

advertisement
  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

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

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn