Code for Program to show an example of extracting subStrings from a String in Java
publicclass JAVA_052
{
publicstaticvoid main(String[] args)
{
String Text="To be or not to be";
System.out.println("The String is : " + Text + "\n");
int count=0;
int index=0;
char Separator=' ';
do
{
++count;
++index;
index=Text.indexOf(Separator,index);
}
while(index!=-1);
String[] subStr=new String[count];
int endIndex=0;
index=0;
for(int i=0;i<count;i++)
{
endIndex=Text.indexOf(Separator,index);
if(endIndex==-1)
subStr[i]=Text.substring(index);
else
subStr[i]=Text.substring(index,endIndex);
index=(endIndex+1);
}
System.out.println("The extracted subStrings are :");
for(int j=0;j<count;j++)
System.out.println(subStr[j]);
}
}