Help me!
Hi:
My code is:
String str="hello.jsp";
String[] temp=str.split(".",2);
System.out.println(temp[0]);
System.out.println(temp[1]);
When I run this code,the compiler told me a error in "
str.split(".",2)".
Hi:
My code is:
String str="hello.jsp";
String[] temp=str.split(".",2);
System.out.println(temp[0]);
System.out.println(temp[1]);
When I run this code,the compiler told me a error in "
str.split(".",2)".
jack,
Pl see below code that works. There is no method called split which you tried to use. Instead there is a class StringTokenizer which is very useful for splitting up Strings with a delimiter. Cheers.
import java.util.*;
public class TestStr
{
public static void main (String [] args)
{
String str = "hello.jsp";
StringTokenizer st = new StringTokenizer(str, ".");
int num = st.countTokens();
String [] arr = new String [num];
for (int i=0; i<num; i++)
{
arr [ i ] = st.nextToken();
System.out.println(arr [ i ]);
}
}
}
>
even something easier
why not using substring class
say
you have
String blah =" Hello World";
then you could just say
System.out.println(blah.substring(0,5));
which would print
Hello
if you only wanne print World
you could say
System.out.println(blah.susbstring(5));
not the most smart way of doing it but it does the job and its easier to understand then Tokenizer =) for a begginer i mean