‘IsNullOrWhitespace’ in JavaScript?

For a succinct modern cross-browser implementation, just do: function isNullOrWhitespace( input ) { return !input || !input.trim(); } Here’s the jsFiddle. Notes below. The currently accepted answer can be simplified to: function isNullOrWhitespace( input ) { return (typeof input === ‘undefined’ || input == null) || input.replace(/\s/g, ”).length < 1; } And leveraging falsiness, even … Read more

Checking if a collection is empty in Java: which is the best method?

You should absolutely use isEmpty(). Computing the size() of an arbitrary list could be expensive. Even validating whether it has any elements can be expensive, of course, but there’s no optimization for size() which can’t also make isEmpty() faster, whereas the reverse is not the case. For example, suppose you had a linked list structure … Read more

Check string for nil & empty

If you’re dealing with optional Strings, this works: (string ?? “”).isEmpty The ?? nil coalescing operator returns the left side if it’s non-nil, otherwise it returns the right side. You can also use it like this to return a default value: (string ?? “”).isEmpty ? “Default” : string!

Detect if an input has text in it using CSS — on a page I am visiting and do not control?

You can use the :placeholder-shown pseudo class. Technically a placeholder is required, but you can use a space instead. input:not(:placeholder-shown) { border-color: green; } input:placeholder-shown { border-color: red; } <input placeholder=”Text is required” /> <input placeholder=” ” value=”This one is valid” /> <input placeholder=” ” />

What is the best way to test for an empty string in Go?

Both styles are used within the Go’s standard libraries. if len(s) > 0 { … } can be found in the strconv package: http://golang.org/src/pkg/strconv/atoi.go if s != “” { … } can be found in the encoding/json package: http://golang.org/src/pkg/encoding/json/encode.go Both are idiomatic and are clear enough. It is more a matter of personal taste and … Read more