lundi 23 novembre 2015

Check if pattern exists in a String

I would like to check if a pattern exists in a String using iteration.

Here is what I have currently but I keep getting false as a result of it.

public static void main(String args[]) {
        String pattern = "abc";
        String letters = "abcdef";

        char[] patternArray = pattern.toCharArray();
        char[] lettersArray = letters.toCharArray();

        for(int i = patternArray.length - 1; i<= 2; i++){
            for(int j = lettersArray.length - 1; j <= 5;j++){
                if(patternArray[i] == lettersArray[j]){
                    System.out.println("true");
                } else{
                    System.out.println("false");
                }
            }
        }
    }

Basically I would like to check if abc exists in the String abcdef

Note: I don't want to use regex because is too easy. I am trying to find a solution without it because I am curious how to do it with iteration.

Aucun commentaire:

Enregistrer un commentaire