StringTokenizer is a java object which is available in java.util package. Using this object we can split the String into multiple chunks and have it used as per the business use case.
We can split the String by providing any delimiters to it or default delimiter is "space". Let's have a look at the below example.
Output:
We can split the String by providing any delimiters to it or default delimiter is "space". Let's have a look at the below example.
import java.util.StringTokenizer;
public class StringTokenizerTest {
public static void main(String[] args) {
String test = "This is an : example string: we are going : to split it";
StringTokenizer st1 = new StringTokenizer(test);
System.out.println("---- Split by space(by default) ------");
while (st1.hasMoreElements()) {
System.out.println(st1.nextElement());
}
System.out.println("---- Split by colon ':' ------");
StringTokenizer st2 = new StringTokenizer(test, ":");
while (st2.hasMoreElements()) {
System.out.println(st2.nextElement());
}
}
}
Output:
---- Split by space(by default) ------
This
is
an
:
example
string:
we
are
going
:
to
split
it
---- Split by colon ':' ------
This is an
example string
we are going
to split it
No comments:
Post a Comment