Find Index of String Occurrence - Java Method Help

In summary, the conversation is about a person needing help with a Java method involving strings and returning the index of the first occurrence of one string in another. They are not allowed to use the indexOf method and are looking for an alternative solution. Suggestions are made to limit the search indexes and to create a string compare function. The issue with the code is identified as using text1 for two different purposes.
  • #1
JaysFan31
Hi. I just started Java and need some help with a method involving strings.

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:
Code:
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;
}
}

NOTE: I cannot use the indexOf method. It's for a programming class at a local university and he won't let us use it. So I need to find an alternative way to basically do what it does.

My code compiles; it's just wrong semantically. Any help would be appreciated.
 
Technology news on Phys.org
  • #2
Search indexes should be limited to length1 - length2, no point in searching past the point where there aren't enough bytes remaining in text1. If length1 < length2, then no match is possible.

Since this is just a test program, might as well create a string compare function, and then call it to compare &text1[test index], with text2, length of test2, until you get a match or increment test index > (length1-length2).
 
  • #3
I think the basic problem is you are trying to use text1 for two different things. When you assign text1 to a substring of itself, you delete the end of the original string. For example you won't find xyz in abxcdxyz, because you find the first x and then overwrite the long string with xcd.
 
Last edited:

What is the purpose of the "Find Index of String Occurrence - Java Method"?

The purpose of this Java method is to search for the first occurrence of a specific string within a given string and return its index position. This can be useful for manipulating strings or searching for specific patterns within a larger string.

How do I use the "Find Index of String Occurrence - Java Method"?

To use this method, you will need to provide two parameters: the string you want to search within, and the string you want to search for. The method will then return an integer value representing the index position of the first occurrence of the search string within the given string. If the search string is not found, the method will return -1.

Can the "Find Index of String Occurrence - Java Method" be used for case-sensitive searches?

Yes, by default this method is case-sensitive. This means that if you are searching for a string with uppercase or lowercase letters, it will only match strings with the exact same casing. To make the search case-insensitive, you can use the toLowerCase() or toUpperCase() methods on both the given string and the search string before passing them to the method.

What happens if the given string or search string is empty or null?

If either the given string or the search string is empty or null, the method will return -1. It is important to check for empty or null values before calling this method to avoid unexpected results.

Can I use this method to find multiple occurrences of a search string within a given string?

No, this method will only return the index position of the first occurrence of the search string. If you need to find multiple occurrences, you can use a loop or a regular expression to iterate through the string and find all matches.

Similar threads

  • Programming and Computer Science
Replies
5
Views
882
  • Programming and Computer Science
Replies
1
Views
872
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
32
Views
2K
  • Programming and Computer Science
2
Replies
66
Views
4K
Replies
10
Views
958
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
3
Replies
75
Views
4K
Back
Top