is-empty

Check string for nil & empty

南笙酒味 提交于 2019-11-28 03:17:08
Is there a way to check strings for nil and "" in Swift? In Rails, I can use blank() to check. I currently have this, but it seems overkill: if stringA? != nil { if !stringA!.isEmpty { ...blah blah } } 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! Ryan You could perhaps use the if-let-where clause: Swift 3: if let string = string, !string.isEmpty { /* string

How to convert empty spaces into null values, using SQL Server?

只谈情不闲聊 提交于 2019-11-27 21:06:15
问题 I have a table and the columns on this table contains empty spaces for some records. Now I need to move the data to another table and replace the empty spaces with a NULL value. I tried to use: REPLACE(ltrim(rtrim(col1)),' ',NULL) but it doesn't work. It will convert all of the values of col1 to NULL . I just want to convert only those values that have empty spaces to NULL . 回答1: Did you try this? UPDATE table SET col1 = NULL WHERE col1 = '' As the commenters point out, you don't have to do

ValueError when checking if variable is None or numpy.array

寵の児 提交于 2019-11-27 20:14:53
问题 I'd like to check if variable is None or numpy.array. I've implemented check_a function to do this. def check_a(a): if not a: print "please initialize a" a = None check_a(a) a = np.array([1,2]) check_a(a) But, this code raises ValueError. What is the straight forward way? ValueError Traceback (most recent call last) <ipython-input-41-0201c81c185e> in <module>() 6 check_a(a) 7 a = np.array([1,2]) ----> 8 check_a(a) <ipython-input-41-0201c81c185e> in check_a(a) 1 def check_a(a): ----> 2 if not

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

此生再无相见时 提交于 2019-11-27 18:15:27
I have two ways of checking if a List is empty or not if (CollectionUtils.isNotEmpty(listName)) and if (listName != null && listName.size() != 0) My arch tells me that the former is better than latter. But I think the latter is better. Can anyone please clarify it? Jon Skeet 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

VBA Check if variable is empty

僤鯓⒐⒋嵵緔 提交于 2019-11-27 17:23:39
I have an object and within it I wanna check if some properties is set to false, like: If (not objresult.EOF) Then 'Some code End if But somehow, sometimes objresult.EOF is Empty , and how can I check it? IsEmpty function is for excel cells only objresult.EOF Is Nothing - return Empty objresult.EOF <> null - return Empty as well! Oorang How you test depends on the Property's DataType: | Type | Test | Test2 | Numeric (Long, Integer, Double etc.) | If obj.Property = 0 Then | | Boolen (True/False) | If Not obj.Property Then | If obj.Property = False Then | Object | If obj.Property Is Nothing Then

How to find whether or not a variable is empty in Bash

两盒软妹~` 提交于 2019-11-27 16:44:53
How can I check if a variable is empty in Bash? Jay In bash at least the following command tests if $var is empty : if [[ -z "$var" ]]; then #do what you want fi The command man test is your friend. ChristopheD Presuming bash: var="" if [ -n "$var" ]; then echo "not empty" else echo "empty" fi Daniel Andersson I have also seen if [ "x$variable" = "x" ]; then ... which is obviously very robust and shell independent. Also, there is a difference between "empty" and "unset". See How to tell if a string is not defined in a bash shell script? . alexli if [ ${foo:+1} ] then echo "yes" fi prints yes

Why is String.IsNullOrEmpty faster than String.Length?

倖福魔咒の 提交于 2019-11-27 13:42:53
问题 ILSpy shows that String.IsNullOrEmpty is implemented in terms of String.Length . But then why is String.IsNullOrEmpty(s) faster than s.Length == 0 ? For example, it's 5% faster in this benchmark: var stopwatches = Enumerable.Range(0, 4).Select(_ => new Stopwatch()).ToArray(); var strings = "A,B,,C,DE,F,,G,H,,,,I,J,,K,L,MN,OP,Q,R,STU,V,W,X,Y,Z,".Split(','); var testers = new Func<string, bool>[] { s => s == String.Empty, s => s.Length == 0, s => String.IsNullOrEmpty(s), s => s == "" }; int

'IsNullOrWhitespace' in JavaScript?

霸气de小男生 提交于 2019-11-27 12:33:21
Is there a JavaScript equivalent to .NET's String.IsNullOrWhitespace so that I can check if a textbox on the client-side has any visible text in it? I'd rather do this on the client-side first than post back the textbox value and rely only on server-side validation, even though I will do that as well. It's easy enough to roll your own : function isNullOrWhitespace( input ) { if (typeof input === 'undefined' || input == null) return true; return input.replace(/\s/g, '').length < 1; } For a succinct modern cross-browser implementation, just do: function isNullOrWhitespace( input ) { return

How do I detect empty cells in a cell array?

可紊 提交于 2019-11-27 11:32:25
问题 How do I detect empty cells in a cell array? I know the command to remove the empty cell is a(1) = [] , but I can't seem to get MATLAB to automatically detect which cells are empty. Background: I preallocated a cell array using a=cell(1,53) . Then I used if exist(filename(i)) and textscan to check for a file, and read it in. As a result, when the filename(i) does not exist, an empty cell results and we move onto the next file. When I'm finished reading in all the files, I would like to delete

How to check if a file is empty in Bash?

那年仲夏 提交于 2019-11-27 10:42:12
I have a file called diff.txt. Want to check if it is empty. Did something like this but couldn't get it working. if [ -s diff.txt ] then touch empty.txt rm full.txt else touch full.txt rm emtpy.txt fi Misspellings are irritating, aren't they? Check your spelling of empty , but then also try this: #!/bin/bash -e if [ -s diff.txt ] then rm -f empty.txt touch full.txt else rm -f full.txt touch empty.txt fi I like shell scripting a lot, but one disadvantage of it is that the shell cannot help you when you misspell, whereas a compiler like your C++ compiler can help you. Notice incidentally that I