What is the correct way to check for string equality in JavaScript?

always Until you fully understand the differences and implications of using the == and === operators, use the === operator since it will save you from obscure (non-obvious) bugs and WTFs. The “regular” == operator can have very unexpected results due to the type-coercion internally, so using === is always the recommended approach. For insight … Read more

Convert a String representation of a Dictionary to a dictionary

You can use the built-in ast.literal_eval: >>> import ast >>> ast.literal_eval(“{‘muffin’ : ‘lolz’, ‘foo’ : ‘kitty’}”) {‘muffin’: ‘lolz’, ‘foo’: ‘kitty’} This is safer than using eval. As its own docs say: >>> help(ast.literal_eval) Help on function literal_eval in module ast: literal_eval(node_or_string) Safely evaluate an expression node or a string containing a Python expression. The string … Read more

StringBuilder vs String concatenation in toString() in Java

Version 1 is preferable because it is shorter and the compiler will in fact turn it into version 2 – no performance difference whatsoever. More importantly given we have only 3 properties it might not make a difference, but at what point do you switch from concat to builder? At the point where you’re concatenating … Read more

Converting from a string to boolean in Python

Really, you just compare the string to whatever you expect to accept as representing true, so you can do this: s == ‘True’ Or to checks against a whole bunch of values: s.lower() in [‘true’, ‘1’, ‘t’, ‘y’, ‘yes’, ‘yeah’, ‘yup’, ‘certainly’, ‘uh-huh’] Be cautious when using the following: >>> bool(“foo”) True >>> bool(“”) False … Read more

How do I check if string contains substring? [duplicate]

Like this: if (str.indexOf(“Yes”) >= 0) …or you can use the tilde operator: if (~str.indexOf(“Yes”)) This works because indexOf() returns -1 if the string wasn’t found at all. Note that this is case-sensitive. If you want a case-insensitive search, you can write if (str.toLowerCase().indexOf(“yes”) >= 0) Or: if (/yes/i.test(str)) The latter is a regular expression … Read more

endsWith in JavaScript

UPDATE (Nov 24th, 2015): This answer is originally posted in the year 2010 (SIX years back.) so please take note of these insightful comments: Shauna – Update for Googlers – Looks like ECMA6 adds this function. The MDN article also shows a polyfill. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith T.J. Crowder – Creating substrings isn’t expensive on modern browsers; it … Read more

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