jerbo
Junior Poster in Training
84 posts since Sep 2004
Reputation Points: 11
Solved Threads: 1
you can also use the indexOf method of the string class ... see the String APIs for more reference.
nanosani
Unauthenticated Liar
1,830 posts since Jul 2004
Reputation Points: 45
Solved Threads: 56
but if it's "ee" you want as result:
int pos = firstString.indexOf("ee");
System.out.println(firstString.substring(pos, pos+2));
stultuske
Posting Sensei
3,135 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
if it is only "ee" then use
String s=firstString.substring(8,10);
System.out.println(s);
it will give the output "ee"
but if you would want any arbitary substring then your opinion is correct.
But then if you want an arbitary substring of three or more characters then it would not work, and you would be writing firstString.substring(pos,pos+3) or String.substring(pos,pos+4).
Anyway, thanks for sending me your opinion about this.
String originalString = "original String";
String toFind = "rig"; // can be entered by user
int pos = originalString.indexOf(toFind);
System.out.println(originalString.substring(pos, pos + toFind.length());
toFind = "St"; // new String chosen by user
pos = originalString.indexOf(toFind);
System.out.println(originalString.substring(pos, pos + toFind.length());
use it like that, and you can change both the 'originalString' as the 'toFind', without your code becoming useless.
stultuske
Posting Sensei
3,135 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433