Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I make startswith function in Java

public static boolean startsWith(String x, String y) {
    int check = 0;
    for (int i = 0; i < y.length(); i++) {
        if (y[i] == x[i]) {
            check += 1;
        }
        if (check == y.length()) {
            return true;
        }
        return false;
    }
}

I tried to make startswith function alternative but it’s not working.
I put all bracelets on the right places but it still gives me an error.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public static boolean startsWith(String s, String prefix) {
    if (prefix.length() <= s.length()) {
        String sub = s.substring(0, prefix.length());
        return sub.equals(prefix);
        //Alternatively
        /*
        for (int i = 0; i < sub.length(); i++) {
            if (sub.charAt(i) != prefix.charAt(i)) {
                return false;
            }
        }
        return true;
        */
    }
    return false;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading