开发者

String is null or empty

Ok, here are some easy points. PyBinding came with this script:

def IsNotNull(value):
    return value is not N开发者_StackOverflow中文版one

It is close, but what I want is this.

bool IsNotNullOrEmpty(string value) {
    return (value != null) && (value.Length > 0 );
}


To check if a string is empty you would use len. Try this:

def IsNotNull(value):
    return value is not None and len(value) > 0


You should not be doing this in a function. Instead you should just use:

if someStringOrNone:


If it's IronPython, then why not use the default implementation of IsNullOrEmpty from System.String?

import clr
clr.AddReference('System')
import System
System.String.IsNullOrEmpty('') # returns True
System.String.IsNullOrEmpty(None) # returns True
System.String.IsNullOrEmpty('something') # returns False


def IsNotNullString(s):
    return bool(s)

Rules of Python boolean conversion.


i think,

if IsNotNull(value) {

is equivalent to

if not value:

for strings. so i think the function is not necessary in python.


if not value or len(value)==0:
    return True 
else:
    return False

Try this one. I recommend you to read this book:https://play.google.com/store/apps/details?id=com.gavin.gbook

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜