What’s the difference between str.isdigit(), isnumeric() and isdecimal() in Python?

By definition, isdecimal() ⊆ isdigit() ⊆ isnumeric(). That is, if a string is decimal, then it’ll also be digit and numeric. Therefore, given a string s and test it with those three methods, there’ll only be 4 types of results. +————-+———–+————-+———————————-+ | isdecimal() | isdigit() | isnumeric() | Example | +————-+———–+————-+———————————-+ | True | True … Read more

Escape string to use in mail()

The idea behind email-injection is that attacker inject line feed (LF) in the email headers and so he adds as many headers as he wants. Stripping those line feeds will protect you from this attack. For detailed info check http://www.phpsecure.info/v2/article/MailHeadersInject.en.php The best practice is to rely on a well-written, frequently updated and widely-used code. For … Read more

How can I match an exact word in a string?

For this kind of thing, regexps are very useful : import re print(re.findall(‘\\blocal\\b’, “Hello, locally local test local.”)) // [‘local’, ‘local’] \b means word boundary, basically. Can be space, punctuation, etc. Edit for comment : print(re.sub(‘\\blocal\\b’, ‘*****’, “Hello, LOCAL locally local test local.”, flags=re.IGNORECASE)) // Hello, ***** locally ***** test *****. You can remove flags=re.IGNORECASE … Read more

Does concatenating strings in Java always lead to new strings being created in memory?

I realized that the second way uses string concatenation and will create 5 new strings in memory and this might lead to a performance hit. No it won’t. Since these are string literals, they will be evaluated at compile time and only one string will be created. This is defined in the Java Language Specification … Read more

Why is modifying a string through a retrieved pointer to its data not allowed?

Why can’t we write directly to this buffer? I’ll state the obvious point: because it’s const. And casting away a const value and then modifying that data is… rude. Now, why is it const? That goes back to the days when copy-on-write was considered a good idea, so std::basic_string had to allow implementations to support … Read more