當Python字串遇上MySQL
學習的時候我喜歡對比,MySQL和Oracle比,Python和MySQL比,總能有一些收穫,也有了新的理解。
今天整理這部分內容的時候,我發現Python和MySQL還是有很多相似之處。學習一門語言,一個資料庫,字串的處理都是一個相對重要的部分,所以我決定對比一下兩者的差別。
下面的演示會一邊Python,一邊MySQL,所以按照這個思路來看就不會感覺突兀了。
跳脫字元
>>> print '\\'
\
mysql> select '\\';
+---+
| \ |
+---+
| \ |
+---+
>>> print '\"'
"
mysql> select '\"';
+---+
| " |
+---+
| " |
+---+
>>> print '\''
'
mysql> select '\'';
+---+
| ' |
+---+
| ' |
+---+
字串拼接
>>> x = 'hello'
>>> y = 'tester'
>>> z = x + y
>>> print z
hellotester
set @x='hello';
set @y='tester';
mysql> select @x;
+-------+
| @x |
+-------+
| hello |
mysql> select @y;
+--------+
| @y |
+--------+
| tester |
+--------+
mysql> select concat(@x,@y);
+---------------+
| concat(@x,@y) |
+---------------+
| hellotester |
+---------------+
字串複製
>>> print '#'*20
####################
mysql> select repeat('#',20);
+----------------------+
| repeat('#',20) |
+----------------------+
| #################### |
+----------------------+
>>> print ' '*20 + 'end'
end
mysql> select space(20);
+----------------------+
| space(20) |
+----------------------+
| |
+----------------------+
字串擷取
>>> name = 'yangjianrong'
>>> name[0]
'y'
>>> name[-1]
'g'
>>> name[1]
'a'
>>> name[1:4]
'ang'
>>> name[:]
'yangjianrong'
>>>
>>> name[1:4:2]
'ag'
mysql> set @name:='yangjianrong';
mysql> select left(@name,1);
+---------------+
| left(@name,1) |
+---------------+
| y |
+---------------+
mysql> select right(@name,1);
+----------------+
| right(@name,1) |
+----------------+
| g |
+----------------+
mysql> select substring(@name,2,3);
+----------------------+
| substring(@name,2,3) |
+----------------------+
| ang |
+----------------------+
mysql> select substring(@name,1);
+--------------------+
| substring(@name,1) |
+--------------------+
| yangjianrong |
+--------------------+
或者使用mid
mysql> select mid(@name,2,3);
+----------------+
| mid(@name,2,3) |
+----------------+
| ang |
+----------------+
mysql> select mid(@name,1);
+--------------+
| mid(@name,1) |
+--------------+
| yangjianrong |
+--------------+
>>> name
'yangjianrong'
>>> print '%s' %name
yangjianrong
字串格式化,匹配
>>> '{name},{alias}'.format(name='yangjianrong',alias='jeanron100')
'yangjianrong,jeanron100'
>>>
mysql> select concat(insert(@name,1,4,'yangjianrong'),insert(@alias,1,5,'jeanron100')) comm;
+------------------------+
| comm |
+------------------------+
| yangjianrongjeanron100 |
+------------------------+
字串長度>>> ba
'this is a test bar'
>>> len(ba)
18
mysql> select length(@ba);
字串空格處理
>>> s = ' abc '
>>> s.lstrip()
'abc '
>>> s.rstrip()
' abc'
>>> s.strip()
'abc'
>>>
mysql> set @s=' abc ';
Query OK, 0 rows affected (0.00 sec)
mysql> select ltrim(@s);
+-----------+
| ltrim(@s) |
+-----------+
| abc |
+-----------+
1 row in set (0.00 sec)
mysql> select rtrim(@s);
+-----------+
| rtrim(@s) |
+-----------+
| abc |
+-----------+
1 row in set (0.00 sec)
mysql> select trim(@s);
+----------+
| trim(@s) |
+----------+
| abc |
+----------+
1 row in set (0.00 sec)
字串匹配
>>> l = ['a','b','c']
>>> ''.join(l)
'abc'
>>> '*'.join(l)
'a*b*c'
mysql> select concat_ws(',','a','b','c','d','e') comm;
+-----------+
| comm |
+-----------+
| a,b,c,d,e |
+-----------+
>>> s = 'a b c d e '
>>> s.split(' ')
['a', 'b', 'c', 'd', 'e', '']
mysql> set @s='a b c d e ';
Query OK, 0 rows affected (0.00 sec)
mysql> select replace(@s,' ',',');
+---------------------+
| replace(@s,' ',',') |
+---------------------+
| a,b,c,d,e, |
+---------------------+
字串複製
>>> s = 'aabbcc'
>>> s.replace('aa','tt')
'ttbbcc'
mysql> set @s='aabbcc';
Query OK, 0 rows affected (0.00 sec)
mysql> select replace(@s,'aa','tt');
+-----------------------+
| replace(@s,'aa','tt') |
+-----------------------+
| ttbbcc |
+-----------------------+
字串編碼
>>> s.encode('utf8')
'aabbcc'
mysql> select convert(@s using utf8);
+------------------------+
| convert(@s using utf8) |
+------------------------+
| aabbcc |
+------------------------+
判斷字串開始匹配的字元
>>> s.startswith('aa')
True
mysql> SELECT LOCATE('aa',@s,1);
+-------------------+
| LOCATE('aa',@s,1) |
+-------------------+
| 1 |
+-------------------+
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29829936/viewspace-2149011/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 當 Rust 遇上 FedoraRust
- 當UIColor遇上SwiftUISwift
- 當 React 遇上 KendoUIReactUI
- 當UIColor遇上 SwiftUISwift
- 當transition遇上display
- 當 Go 遇上了 LuaGo
- 當 Go struct 遇上 MutexGoStructMutex
- 當 bind 遇上 applyAPP
- 老遊戲遇上新問題:當動森遇上詐騙遊戲
- 當 Python 和 R 遇上北京二手房Python
- 當Shell遇上了NodeJSNodeJS
- 當區塊鏈遇上保險區塊鏈
- 當go get遇上gitlabGoGitlab
- 當微信小程式遇上filter~微信小程式Filter
- 當好萊塢遇上國產電影
- 當使用者管理系統遇上python和mongodb後……PythonMongoDB
- 當新零售遇上 ServerlessServer
- 當12C PDB遇上JDBCJDBC
- 當 JS 遇上物聯網(IoT)JS
- 天生一對,當遊戲遇上MongoDB遊戲MongoDB
- SOA Agents──當網格遇上SOA
- 當動態桌面遇上 HTML5HTML
- Go死鎖——當Channel遇上Mutex時GoMutex
- [譯] 當設計模式遇上 Kotlin設計模式Kotlin
- 當 mysql-connector-java-5 遇上 MySQL8,終究還是錯付了 → 門當戶對真的很重要!MySqlJava
- 當催收遇上疫情,AI能做些什麼?AI
- 當頁面渲染遇上邊緣計算
- 當餐飲遇上大資料,嗯真香!大資料
- 當金融行業遇上開源技術行業
- 當資料探勘遇上戰略決策
- 當 jenkins遇上Android Studio 3.0JenkinsAndroid
- 當「軟體研發」遇上 AI 大模型AI大模型
- 【新特性速遞】當法語遇上FineUI(Bonjour)!UI
- 當JSON.parse“遇上”非鍵值對JSON
- 當DUBBO遇上Arthas - 排查問題的實踐
- 當 Swagger 遇上 Torna,瞬間高大上了!Swagger
- 當 RocketMQ 遇上 Serverless,會碰撞出怎樣的火花?MQServer
- 當RPA遇上AI,會擦出怎樣的火花?AI