001_024 Python 让某些字符串大小写不敏感 如比较和查询不敏感 其他敏感
2014-03-12 19:37
399 查看
代码如下:
打印结果如下:
中国
True
False
#encoding=utf-8 print '中国' #让某些字符串大小写不敏感 如比较和查询不敏感 其他敏感 #方案 封装为类 class iStr(str): '''大小写不敏感的字符串类 类似str 比较和查询大小写不敏感 ''' def __init__(self,*args): self.lowered = str.lower(self) def __repr__(self): return '%s(%s)' % (type(self).__name__,str.__repr__(self)) def __hash__(self): return hash(self.lowered) def _make_case_insensitive(name): str_meth = getattr(str,name) def x(self,other,*args): try: other = other.lower() except(TypeError, AttributeError, ValueError) :pass return str_meth(self.lowered,other,*args) setattr(iStr,name,x) for name in 'eq lt le ge gt ne contains'.split(): _make_case_insensitive('__%s__' % name) for name in 'count endswith find index rfind rindex startswith'.split(): _make_case_insensitive(name) del _make_case_insensitive a = iStr('abcA') b = iStr('aBca') print a == b a = str('abcA') b = str('aBca') print a == b
打印结果如下:
中国
True
False
相关文章推荐
- 黄聪:Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- shell 脚本中实现字符串大小写不敏感的比较方法
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- Python字符串比较忽略大小写的方法
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- 转 Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- mysql连接字符串,大小写不敏感查询
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)