66.Python中startswith和endswith的使用
定义模型的models.py,示例代码如下:
from django.db import models
class Category(models.Model):
name = models.CharField(max_length=100)
class Meta:
db_table = 'category'
class Article(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
category = models.ForeignKey('Category', on_delete=models.CASCADE, null=True)
def __str__(self):
return "<(Article: id: %s,title: %s, content: %s)>" % (self.id, self.title, self.content)
class Meta:
db_table = 'article'
1. startswith:大小写敏感的判断某个字段的值是否以某个值开始的。示例代码如下:
from .models import Article, Category
from django.http import HttpResponse
def index(request):
articles = Article.objects.filter(title__startswith='hello')
print(articles)
print(articles.query)
return HttpResponse("success")
首先,查看数据库表中的数据如下:

打印出结果:
<QuerySet []>:返回的QuerySet为空。
SELECT article.id, article.title, article.content, article.category_id FROM article WHERE article.title LIKE BINARY hello%. 需要注意的是这里执行的sql语句为LIKE BINARY hello%,LIKE BINARY代表的是区分大小写,hello%代表的是以hello为开头,%代表的是后面可以匹配任意多个字符。
2.istartswith: 大小写不敏感的判断某个字段的值是否以某个值开始的,示例代码如下:
from .models import Article, Category
from django.http import HttpResponse
def index(request):
articles = Article.objects.filter(title__istartswith='hello')
print(articles)
print(articles.query)
return HttpResponse("success")
打印出结果如下:
<QuerySet [<Article: <(Article: id: 1,title: Hello, content: 你好)>>, <Article: <(Article: id: 2,title: Hello World, content: 大家好)>>]> : 查找出两条符合条件的文章。
SELECT article.id, article.title, article.content, article.category_id FROM article WHERE article.title **LIKE hello%: 需要注意的是,这里将查询条件翻译成了sql语句为:article.title LIKE hello%,这里的LIKE代表的是不区分大小写,hello%代表的是以hello开头,后面可以匹配任意多个字符。
3.endswith: 大小写敏感的判断某个字段的值中是否含有某个值开始。示例代码如下:
from .models import Article, Category
from django.http import HttpResponse
def index(request):
articles = Article.objects.filter(title__endswith='world')
print(articles)
print(articles.query)
return HttpResponse("success")
打印出结果如下:
<QuerySet []> 输出的结果为空的QuerySet。
SELECT article.id, article.title, article.content, article.category_id FROM article WHERE article.title LIKE BINARY %world :这里的查询条件被翻译article.title LIKE BINARY %world,LIKE BINARY代表的是区分大小写的判断,%world代表的是以world为结束,前面可以匹配任意多个字符,如果可以满足条件就会返回。
4.iendswith: 不区分大小写判断某个字段的值中是否含有某个值,示例代码如下:
from .models import Article, Category
from django.http import HttpResponse
def index(request):
articles = Article.objects.filter(title__iendswith='world')
print(articles)
print(articles.query)
return HttpResponse("success")
打印出结果如下所示:
<QuerySet [<Article: <(Article: id: 2,title: Hello World, content: 大家好)>>]>: 返回了一个满足条件的文章
SELECT article.id, article.title, article.content, article.category_id FROM article WHERE article.title LIKE %world: 需要注意的是,这里为 LIKE,代表的是不区分大小写的判断,并且以world结尾,world前面可以匹配任意多个字符,满足条件才会被返回。
66.Python中startswith和endswith的使用的更多相关文章
- python 中startswith()和endswith() 方法
startswith()方法 Python startswith() 方法用于检查字符串是否是以指定子字符串开头如果是则返回 True,否则返回 False.如果参数 beg 和 end 指定值,则在 ...
- Python的startswith和endswith
做文本处理的时候经常要判断一个文本有没有以一个子串开始,或者结束.Python为此提供了两个函数: S.startswith(prefix[, start[, end]]) -> bool 如果 ...
- Python中的startswith和endswith函数使用实例
Python中的startswith和endswith函数使用实例 在Python中有两个函数分别是startswith()函数与endswith()函数,功能都十分相似,startswith()函数 ...
- python中strip、startswith、endswith
strip(rm)用来删除元素内的空白符: rm对应要删除空白符的元素,当rm为空(strip())时删除所有元素的空白符 startswith.endswith用来查找开头或结尾条件的元素 例子: ...
- 【C++实现python字符串函数库】二:字符串匹配函数startswith与endswith
[C++实现python字符串函数库]字符串匹配函数startswith与endswith 这两个函数用于匹配字符串的开头或末尾,判断是否包含另一个字符串,它们返回bool值.startswith() ...
- Python: 字符串开头或结尾匹配str.startswith(),str.endswith()
问题 需要通过指定的文本模式去检查字符串的开头或者结尾,比如文件名后缀,URLScheme 等等. 解决方案 1.检查字符串开头或结尾的一个简单方法是使用str.startswith() 或者是str ...
- 在Javascript中使用String.startsWith和endsWith
在Javascript中使用String.startsWith和endsWith 在操作字符串(String)类型的时候,startsWith(anotherString)和endsWith(anot ...
- python startswith与endswith
如果你要用python匹配字符串的开头或末尾是否包含一个字符串,就可以用startswith,和endswith比如:content = 'ilovepython'如果字符串content以ilove ...
- python startswith和endswith
startswith判断文本是否以某个或某几个字符开始; endswith判断文本是否以某个或某几个字符结束; text = 'Happy National Day!' print text.star ...
随机推荐
- 第1节 IMPALA:3、impala软件的下载和linux磁盘的挂载
1. impala安装软件下载: http://archive.cloudera.com/cdh5/repo-as-tarball/5.14.0/ 2. linux磁盘的挂载: [root@node0 ...
- C++面试常见问题——16函数模板的使用
函数模板的使用 函数模板在使用之前必须在外部对函数模板进行初始化. 函数模板的实例化包含两中 1.隐式实例化: template <class T> //没有: T Fun(T a,T b ...
- 012、MySQL取本月第一天日期,取每个月的第一天日期
#取本月第一天 SELECT DATE_ADD( curdate( ), INTERVAL DAY ); #取往后几个月的第一天 , INTERVAL MONTH ); , INTERVAL MONT ...
- 7.11 如何应用Varnish
动态数据缓存 Step 1 修改devault.vcl文件 # This ) # man page for details on VCL syntax and semantics. # # Defau ...
- 用ftp命令实现主机文件批量更新
我们的主机环境是windows 2003,平时程序员访问都喜欢用远程桌面.简单快捷直观.不过我比较喜欢在本地用vim和命令行,这样编辑修改不需要受网络影响. 这种情况下,我本地调试的程序,要经常更新到 ...
- ACM-挑战题之排列生成
题目描述:挑战题之排列生成 一自然数N,设N为3,则关于N的字典序排列为123,132,213,231,312,321.对于一个自然数N(1<= N <= 9 ) , 你要做的便是生成它的 ...
- Golang的运算符-位运算符
Golang的运算符-位运算符 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.位运算符概述 常见的位逻辑运算符: &: 位与运算符,表示AND(表示所有条件都得匹配), ...
- Nachos-Lab3-同步与互斥机制模块实现
源码获取 https://github.com/icoty/nachos-3.4-Lab 内容一:总体概述 本实习希望通过修改Nachos系统平台的底层源代码,达到"扩展同步机制,实现同步互 ...
- cf 785#
23333再次水惨了..(心酸啊) A题呵呵呵呵呵 #include<cstdio> #include<iostream> using namespace std; int m ...
- php.laravel.csrf
概念请自己查 在全局帮助函数库Illuminate\Foundation\helpers.php中有以下几个函数定义,在看过前两个函数实现可以在使用中多少有点帮助. function csrf_fie ...