Find substrings in strings in Java

I'm writing a program to find substrings in strings in Java without using any Java libraries

I wrote a function substring (string STR1, string STR2), as shown below

It applies to the following inputs:

>STR1 = "Rahul" STR2 = "my name is Rahul" > STR1 = "Rahul" STR2 = "Rahul SAH" > str3 = "Rahul" STR2 = "SAH Rahul"

There was a problem when I entered:

>STR1 = "Rahul" STR2 = "Rahul" > STR1 = "Rahul" STR2 = "my name is Sunil"

It enters an infinite loop Anyone can view my code snippets and help me

public static boolean subString(String str1,String str2) {
        boolean found = false;
        int len1 = str1.length();
        int len2 = str2.length();
        int status = 0;
        char[] arr1 = new char[len1];
        char[] arr2 = new char[len2];
        for (int ii = 0; ii < len1; ii++) {
            arr1[ii] = str1.charAt(ii);
        }
        for (int jj = 0; jj < len2; jj++) {
            arr2[jj] = str2.charAt(jj);
        }
        for (int ii = 0; ii < len1; ii++) {
            for (int jj = 0; jj < len2; jj++) {
                if (arr1[ii] == arr2[jj]) {
                    if (ii < len1 - 1) {
                        System.out.println("Found1::" + "arr1::" + arr1[ii]
                                + "and arr2::" + arr2[jj]);
                        found = true;
                        ii++;
                    } else if (arr1[ii] == arr2[jj] && ii == len1 - 1) {
                        System.out.println("Found2::" + "arr1::" + arr1[ii]
                                + "and arr2::" + arr2[jj]);
                        found = true;
                        break;
                    }
                } else if (found == false && arr1[ii] != arr2[jj]) {
                    System.out.println("Found3::" + "arr1::" + arr1[ii]
                            + "and arr2::" + arr2[jj]);
                    found = false;

                } else if (found == true && arr1[ii] != arr2[jj]) {
                    System.out.println("Found4::" + "arr1::" + arr1[ii]
                            + "and arr2::" + arr2[jj]);
                    found = false;
                    ii = 0;
                }
            }
        }
        return found;
    }
}

Solution

Others suggest using string Contains () – this is Java Lang code, not Java libraries But you obviously want to explore how to do it yourself One way is to view the string Openjdk 7 source code of contains(), and its cover uses string indexOf(). You can see the (fairly basic) algorithms they use there

There is a problem with your code

Interestingly, when I paste the code into my development environment, your code applies to "Rahul" and "Rahul" However, there are unmatched infinite loops This happens for any STR2 that contains any character of STR1 This is because once a match for any character in STR1 is found in STR2, the variable is reset to start over If you look at its sequence through each string, your output is actually enough to debug it

Possible repairs

If you want to pursue your own method and learn from it, please consider using your own method to stop and design on paper You are looking for STR1 in STR2 So you may want to change your cycle Then you can be more efficient You can view longer string (STR2) characters character by character in an external loop If the first character of the shorter string (STR1) matches the character you processed in STR2, you only need to enter the internal loop

For example, the cyclic bit of the code

boolean retFound = false;

    for (int jj = 0; jj < len2; jj++) {
        if (arr1[0] == arr2[jj]) {
            boolean tempFound = true;
            int foundIndex = jj;
            for (int ii = 0; ii < len1; ii++) {
                if (arr1[ii] != arr2[jj+ii]) {
                    tempFound = false;
                    break;
                 }
             }

             if (tempFound) {
                  System.out.println("Found substring " + str1 + " in " + str2 + " at index " + foundIndex);
                  System.out.println("Carrying on to look for further matches...");
                  tempFound = false;
                  retFound = true;
             }
        }
   }

   return retFound;

Note that this won't be fast, but it should work I have tested all the string samples you provided You also get a bonus - it will find multiple matches If you don't want that (just want true and false), it suddenly erupts when it says "keep looking..."

As others said, if you want to continue using the original code, of course, don't try to change the loop variable (i.e. II) in the inner loop This is a bad practice, difficult to read and prone to a lot of mistakes

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>