Following Example shows how to split the string using str.split(String pattern) method in Java.
Sample Program
01.
package
com.javatutorialcorner.javastring;
02.
03.
public
class
SplitString {
04.
05.
public
static
void
main(String[] args) {
06.
String input1 =
"This is Java string example program"
;
07.
String input2 =
"a-b-c-d-e-f"
;
08.
09.
String[] result1 = input1.split(
" "
);
10.
for
(
int
i =
0
; i < result1.length; i++) {
11.
System.out.println(result1[i]);
12.
}
13.
14.
System.out.println(
"-------------------"
);
15.
String[] result2 = input2.split(
"-"
);
16.
for
(String output : result2) {
17.
System.out.println(output);
18.
}
19.
20.
}
21.
22.
}
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