Wednesday, April 22, 2009

Finding a substring in a string


public static boolean substr(String inputStr, String subStr){
int length = inputStr.length();
int index = 0;
int check = 0;
boolean found = false;
StringBuffer newString = new StringBuffer();
while (index < length) {
try {
if (inputStr.charAt(index) == subStr.charAt(check)) {
newString.append(inputStr.charAt(index));
check++;
if (newString.toString().equals(subStr)) {
found = true;
break;
}
}
} catch (Exception ex){}
index++;
}
return found;
}
Update: The code is also available in Python in this entry

0 comments:

Post a Comment