Java: Literal percent sign in printf statement

The percent sign is escaped using a percent sign: System.out.printf(“%s\t%s\t%1.2f%%\t%1.2f%%\n”,ID,pattern,support,confidence); The complete syntax can be accessed in java docs. This particular information is in the section Conversions of the first link. The reason the compiler is generating an error is that only a limited amount of characters may follow a backslash. % is not a … Read more

Escape special characters in a Python string

Use re.escape >>> import re >>> re.escape(r’\ a.*$’) ‘\\\\\\ a\\.\\*\\$’ >>> print(re.escape(r’\ a.*$’)) \\\ a\.\*\$ >>> re.escape(‘www.stackoverflow.com’) ‘www\\.stackoverflow\\.com’ >>> print(re.escape(‘www.stackoverflow.com’)) www\.stackoverflow\.com Repeating it here: re.escape(string) Return string with all non-alphanumerics backslashed; this is useful if you want to match an arbitrary literal string that may have regular expression metacharacters in it. As of Python 3.7 … Read more

Java – escape string to prevent SQL injection

PreparedStatements are the way to go, because they make SQL injection impossible. Here’s a simple example taking the user’s input as the parameters: public insertUser(String name, String email) { Connection conn = null; PreparedStatement stmt = null; try { conn = setupTheDatabaseConnectionSomehow(); stmt = conn.prepareStatement(“INSERT INTO person (name, email) values (?, ?)”); stmt.setString(1, name); stmt.setString(2, … Read more

How should I escape strings in JSON?

Ideally, find a JSON library in your language that you can feed some appropriate data structure to, and let it worry about how to escape things. It’ll keep you much saner. If for whatever reason you don’t have a library in your language, you don’t want to use one (I wouldn’t suggest this¹), or you’re … Read more

Escaping ampersand character in SQL string

Instead of node_name=”Geometric Vectors \& Matrices” use node_name=”Geometric Vectors ” || chr(38) || ‘ Matrices’ 38 is the ascii code for ampersand, and in this form it will be interpreted as a string, nothing else. I tried it and it worked. Another way could be using LIKE and an underline instead the ‘&’ character: node_name … Read more