I need to return the index of the start of the first occurence of one string (parameter two) in another string (parameter one).
I tried this:
Expand|Select|Wrap|Line Numbers
- public static int findInString (String text1, String text2)
- {
- int length1 = text1.length();
- int length2 = text2.length();
- int index;
- char x = text2.charAt(0);
- for (index = 0; index < length1; index++)
- {
- char y = text1.charAt(index);
- if (x == y)
- {
- text 1 = text1.substring(index, index + length2);
- if (text1 == text2)
- {
- return index;
- }
- }
- if (index == length1)
- {
- return -1;
- }
- }
My code compiles; it's just wrong semantically. Any help would be appreciated.