JAVA: Contains String i list - ekspresowe znajdowanie :)

Jak w temacie, zamiast interować po wszystkim by dowiedzieć się czy coś zawiera się gdzieś, to wystarczy użyć contains(). Oto przykłady:


 public class Test {
    public static void main(String args[]) {
        String str1 = "Java string contains If else Example";
        // In If-else statements you can use the contains() method

        if (str1.contains("example")) {
            System.out.println("The Keyword :example: is found in given string");
        } else {
            System.out.println("The Keyword :example: is not found in the string");
        }
    }
 }



 public class Test {
    public static void main(String args[]) {
        String str1 = "Java string contains If else Example";
        // In If-else statements you can use the contains() method

        if (str1.contains("example")) {
            System.out.println("The Keyword :example: is found in given string");
        } else {
            System.out.println("The Keyword :example: is not found in the string");
        }


        // Initializing a list of type Linkedlist
        List<Integer> l = new LinkedList<>();
        l.add(10);
        l.add(15);
        l.add(20);
        System.out.println(l);

        // Initializing another list
        int element = 20;

        if (l.contains(element))
            System.out.println("Equal");
        else
            System.out.println("Not equal");
    }
 }

Komentarze

Popularne posty z tego bloga

IntelliJ: zmiana rozmiaru czcionki scrollem

ThunderBird: jak zrobić professional stopkę