Click to See Complete Forum and Search --> : String finding and return


mikedikta
10-08-2007, 01:58 PM
Hi, I'm not a java programmer, but I need some help.

could anyone write a method take looks like this:



public String getMatchedString(String text, String start, String end)


the method should recive a string with a text, and the other parameters (start and end) are two string that it must find in the text, when he finds a String that starts with "start" and ends with "end" it should return a string with what is between them.

exemple:

getMatchedString("Name: John Rambo - Age: 21", "Name:" , "- Age: 21")

should return "John Rambo"

got it?

thanks for just reading

Khalid Ali
10-08-2007, 11:42 PM
Its your lucky day..:-) enjoy....


public String getMatchedString(String strToSearchFrom, String strStartsWith, String strEndsWith) {
String result=null;
if(strToSearchFrom.startsWith(strStartsWith) && strToSearchFrom.endsWith(strEndsWith)){
return strToSearchFrom.substring(
strToSearchFrom.indexOf(strStartsWith)+strStartsWith.length(),
strToSearchFrom.indexOf(strEndsWith));
}
return result;
}

mikedikta
10-09-2007, 09:26 AM
thanks mate.