There are so many way to solve this problem but i am giving a very trick solution for this. i am going to use the Scanner class and its method hasNext() and next() to solve this problem.this is simple and trick.so, here is the solution...
import java.util.*;
class
SplitWithoutSplitMethodUsingDelimeter
{
public static void
main(String[] args)
{
String str="hello this is java for every one";
Scanner scn=new Scanner(str);
while(scn.hasNext())
{
System.out.println(scn.next());
}
}
}
Logic of the above program is simple we are taking the string "str" instead of taking "System.in" in Scanner() so it will take input as string value.and after this we are iterating it until it has next element and getting the elements one by one by using next() method which will return the string before space value.when any space appear then next() method get terminate and returns value.
Output:
its fine for the if the string is separated with spaces but when string has no space and separated with other symbol then how to solve this problem.
then for this we have to make a little bit modification in our program so that it could work for any of the symbol .so to get the solution of this problem we will use a method called "useDelimiter(String pattern)" to split the stringg based on any parameter or pattern here is the code snippet...
import java.util.*;
class
SplitWithoutSplitMethodUsingDelimeter
{
public static void
main(String[] args)
{
String str="hello,this,is,java,for,every,one";
Scanner scn=new Scanner(str).useDelimiter(",");
while(scn.hasNext())
{
System.out.println(scn.next());
}
}
}
Note ::
if we are passing the following symbol like (.,+,$ or etc) then we have to use it like "\\." or "\\+" or "\\$" and etc.
Example:
Scanner
scn=new Scanner(str).useDelimiter(",");
Output will same as above code.