Enter your E-mail Address below for Free E-mail Alerts right Into your Inbox: -

Wednesday

How to split string in java

How to split string in java

String is the most and commonly used function in java.String has lot of inbuilt functions and the most common one is split() function. You all java developers know how to use split function in java.
String str = "hai:how:are:the:one";
String str1 = str.split(":");
for(String s:str1)
{
System.out.println(s);
}

For String Tokenizer StringTokenizer the program is much like this.


StringTokenizer str2 = new StringTokenizer(str, ":");
while (str2.hasMoreElements()) {
System.out.println(str2.nextToken());
}


OutPut:

hai
how
are
the
one

By giving the limit:


String str = "hai:how:are:the:one";
String str1 = str.split(":",2);
for(String s:str1)
{
System.out.println(s);
}

OutPut:

hai
how:are:the:one

No comments:

Post a Comment