publicclass JAVA_051
{
publicstaticvoid main(String[] args)
{
String Text="To be or not to be, that is the question; Whether 'this"
+"nobler in the mind to suffer the sling and arrows of "
+"outrageous fortune, or to take arms against a sea of "
+"troubles, and by opposing end them?";
System.out.println("Text paragraph is : \n" + Text + "\n");
String Str="be";
int index=Text.indexOf(Str);
if(index!=-1)
{
System.out.println("From start, first \"" + Str +
"\" is present at the position : " + index);
index+=Str.length( );
}
else
System.out.println("\"" + Str + "\" is not present in the above text.");
index=Text.indexOf(Str,index);
if(index!=-1)
System.out.println("From start, second \"" + Str +
"\" is present at the position : " + index);
else
System.out.println("\"" + Str + "\" is not present in the above text.");
Str="of";
index=Text.lastIndexOf(Str);
if(index!=-1)
{
System.out.println("\nFrom end, first \"" + Str +
"\" is present at the position : " + index);
index-=Str.length( );
}
else
System.out.println("\"" + Str + "\" is not present in the above text.");
index=Text.lastIndexOf(Str,index);
if(index!=-1)
System.out.println("From end, second \"" + Str +
"\" is present at the position : " + index);
else
System.out.println("\"" + Str + "\" is not present in the above text.");
}
}