开发者

Test whether a substring is a part of a string

I made a program to test whether a substring is a part of another string. But it's always returning false.

public class SubString{

    public static void main(String... args){
        String findFrom = "mouse";
        String toFind = "mouse and cat";
        boolean flag = false;
        int toFindLenght = toFind.length();
        int findFromLength = findFrom.length();

        for (int x = 0; x < toFindLenght; x++) {
            char toFindIntermediate = toFind.charAt(x);

            for (int y = 0; y < findFromLength; y++) {
                char toFindFromIntermediate = findFrom.charAt(y);
                int counter = x;
                if (toFindIntermediate == toFindFromIntermediate) {
                    toFindFromIntermediate=toFind.charAt(counter);
                    counter++;

                    flag=true;
                } else {
                    flag=false;
                }
            }
        }

        System.out.println("IS the substring a part o开发者_运维问答f the string :"+flag);
    }
}


That is because you did not break out of the loop, so even if the condition has been set to true in one cycle, it is reset to false in another cycle.


I don't like using break in loops, just how I learned originally I guess, and unless there is some reason I can't have the performance loss from doing 1 extra boolean test, I avoid it. Below is your code with changes commented:

public class SubString {
    public static void main(String... args) {
        String findFrom = "mouse";
        String toFind = "mouse and cat";
        boolean flag = false;
        int toFindLength = toFind.length();
        int findFromLength = findFrom.length(); // fixed spelling of length
        for (int x = 0; x <= toFindLength - findFromLength && !flag; x++) {
            // Changed the test in the for loop above so you won't get
            // index-out-of-bounds exceptions in the next loop.
            // char toFindIntermediate = toFind.charAt(x); // Moving this lower
            flag = true; // this lets you avoid break
            for (int y = 0, counter = x; y < findFromLength && flag; y++) {
                char toFindIntermediate = toFind.charAt(counter);
                char findFromIntermediate = findFrom.charAt(y); 
                // took "to" off above variable name for consistancy
                // int counter = x;
                if (toFindIntermediate == findFromIntermediate) {
                    // When you change the name of the variable you
                    // notice pretty quickly the assignment below is wrong.
                    // Also, you need to increment counter before you store
                    // the next char
                    // findFromIntermediate=toFind.charAt(counter);
                    // counter++;
                    // flag=true;

                    counter++;
                } else {
                    flag=false;
                }
            }
        }
        System.out.println("IS the substring a part of the string :"+flag);
    }
}

And without the comments so it is a little easier to see all at once:

public class SubString {
    public static void main(String... args) {
        String findFrom = "mouse";
        String toFind = "mouse and cat";
        boolean flag = false;
        int toFindLength = toFind.length();
        int findFromLength = findFrom.length();
        for (int x = 0; x <= toFindLength - findFromLength && !flag; x++) {
            flag = true;
            for (int y = 0, int counter = x; y < findFromLength && flag; y++) {
                char toFindIntermediate = toFind.charAt(counter);
                char findFromIntermediate = findFrom.charAt(y); 
                if (toFindIntermediate == findFromIntermediate) {
                    counter++;
                } else {
                    flag=false;
                }
            }
        }
        System.out.println("IS the substring a part of the string :"+flag);
    }
}

That should do what you want while pretty much sticking to your original method. I wouldn't show that to anyone though. For one, your variable names are kind of backwards - normally "toFind" would be the substring you are looking for in a bigger one. Also, you would need to clean up how you are initializing and incrementing variables in loops, just to be consistant throughout your code. You could even switch to while loops and have it be just as clear, if not more so, or go the other direction and move everything in the inner loop into the loop declaration, and have nothing inside it.

Anyway, that should give you a working base to build from.


I am posting here the same code with small change.

here what i have done is i just change the y counter of inner for loop to start from counter and added a break after flag turs true.

Sorry that i didn't check the negative cases. here i'm posting the editted code

public class SubString {
public static void main(String... args) {
    String findFrom = "mouse";
    String toFind = "mouse and cat";
    boolean flag = false;
    int toFindLenght = toFind.length();
    int findFromLength = findFrom.length();
    int counter = 0;
    for (int x = 0; x < toFindLenght; x++) {
        char toFindIntermediate = toFind.charAt(x);
        for (int y = counter; y < findFromLength; y++) {
            char toFindFromIntermediate = findFrom.charAt(y);
            if(toFindIntermediate == ' '){
                break;
            }
            else if (toFindIntermediate == toFindFromIntermediate) {
                toFindFromIntermediate = toFind.charAt(counter);
                counter++;
                flag = true;
                break;
            } else if (0 != counter) {
                flag = false;
                counter = 0;
                break;
            }
        }
    }
    System.out.println("IS the substring a part of the string :" + flag);
}

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜