Test if a string contains any of the strings from an array

EDIT: Here is an update using the Java 8 Streaming API. So much cleaner. Can still be combined with regular expressions too. public static boolean stringContainsItemFromList(String inputStr, String[] items) { return Arrays.stream(items).anyMatch(inputStr::contains); } Also, if we change the input type to a List instead of an array we can use items.stream().anyMatch(inputStr::contains). You can also use … Read more

One line if-condition-assignment

I don’t think this is possible in Python, since what you’re actually trying to do probably gets expanded to something like this: num1 = 20 if someBoolValue else num1 If you exclude else num1, you’ll receive a syntax error since I’m quite sure that the assignment must actually return something. As others have already mentioned, … Read more

Can I use if (pointer) instead of if (pointer != NULL)?

You can; the null pointer is implicitly converted into boolean false while non-null pointers are converted into true. From the C++11 standard, section on Boolean Conversions: A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null … Read more

What is the effect of ordering if…else if statements by probability?

As a general rule, most if not all Intel CPUs assume forward branches are not taken the first time they see them. See Godbolt’s work. After that, the branch goes into a branch prediction cache, and past behavior is used to inform future branch prediction. So in a tight loop, the effect of misordering is … Read more

Bash if statement with multiple conditions throws an error

Use -a (for and) and -o (for or) operations. tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html Update Actually you could still use && and || with the -eq operation. So your script would be like this: my_error_flag=1 my_error_flag_o=1 if [ $my_error_flag -eq 1 ] || [ $my_error_flag_o -eq 2 ] || ([ $my_error_flag -eq 1 ] && [ $my_error_flag_o -eq 2 … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)