java
Java program to split a String using a seperator
We can use String.split() function to split a string into parts and get the Array of the string parts. We can access the Array items using its indexes.
String str = "Java~programming~is~cool";
String[] result = str.split("~");
String p1 = result[0];
String p2 = result[1];
String p3 = result[2];
String p4 = result[3];
System.out.println(p1);
System.out.println(p2);
System.out.println(p3);
System.out.println(p4);
The String.split() method in Java takes the separator string as a parameter and returns the array of elements. In the above code example, we have created a String variable str and we are splitting it using separator '~'.
Was this helpful?
Similar Posts