how to check if a string contains a character

Solutions on MaxInterview for how to check if a string contains a character by the best coders in the world

showing results for - "how to check if a string contains a character"
Lucia
17 Feb 2020
1$a = 'Hello world?';
2
3if (strpos($a, 'Hello') !== false) { //PAY ATTENTION TO !==, not !=
4    echo 'true';
5}
6if (stripos($a, 'HELLO') !== false) { //Case insensitive
7    echo 'true';
8}
Charlotte
28 Jan 2020
1if (string.contains(character)
Ivor
14 Aug 2018
1stringVariable.includes("Your Text");
Noemi
17 May 2017
1 /*
2    This method returns true if the given string contains
3    the char n. It is necessary due to the
4    obsolescence of some compilers, which requires us
5    to write our own contains method.
6    */
7    public static boolean contains_char (String main, char secondary)
8    {
9        boolean contains_result = false;
10        for (int i = 0 ; i < input.length () ; i++)
11        {
12            if (input.charAt (i) == secondary)
13            {
14                contains_result = true;
15                break;
16            }
17        }
18        return contains_result;
19    }
20
21/*
22for two chars:
23*/
24public static boolean contains_dualchar (String main, String secondary)
25    {
26        boolean contains_result = false;
27        for (int i = 0 ; i < input.length () ; i++)
28        {
29            if (input.charAt (i) == secondary.charAt (0))
30            {
31                if (input.charAt (i + 1) == secondary.charAt (1))
32                {
33                    contains_result = true;
34                    break;
35                }
36            }
37        }
38        return contains_result;
39    }
40
Judas
25 Aug 2019
1public static boolean containsIgnoreCase(String str, char c) {
2        str = str.toLowerCase();
3
4        for (int i = 0; i < str.length(); i++) {
5            if (str.charAt(i) == Character.toLowerCase(c)) {
6                return true;
7            }
8        }
9        
10        return false;
11    }
similar questions