Following Example shows how to split the string using str.split(String pattern) method in Java.
Sample Program
package com.javatutorialcorner.javastring;
public class SplitString {
public static void main(String[] args) {
String input1 = "This is Java string example program";
String input2 = "a-b-c-d-e-f";
String[] result1 = input1.split(" ");
for (int i = 0; i < result1.length; i++) {
System.out.println(result1[i]);
}
System.out.println("-------------------");
String[] result2 = input2.split("-");
for (String output : result2) {
System.out.println(output);
}
}
}
Result
The above code will produce the following result.
This
is
Java
string
example
program
-------------------
a
b
c
d
e
f
0 comments:
Post a Comment