学习Python和MySQL之后,发现很多相似之处,结果相同,但实现方式都不太一样。学习一门语言,一个数据库,字符串的处理都是一个相对重要的部分,所以我决定对比一下两者的差别。
下面的演示会一边Python,一边MySQL,所以按照这个思路来看就不会感觉突兀了。
1. 转义字符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
>>> print '\\' \ mysql> select '\\'; +---+ | \ | +---+ | \ | +---+ >>> print '\"' " mysql> select '\"'; +---+ | " | +---+ | " | +---+ >>> print '\'' ' mysql> select '\''; +---+ | ' | +---+ | ' | +---+ |
2. 字符串拼接
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
>>> x = 'hello' >>> y = 'world' >>> z = x + y >>> print(z) helloworld mysql> set @x='hello'; mysql> set @y='tester'; mysql> select concat(@x,@y); +---------------+ | concat(@x,@y) | +---------------+ | helloworld | +---------------+ |
3. 字符串复制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
>>> print('#'*20) #################### mysql> select repeat('#',20); +----------------------+ | repeat('#',20) | +----------------------+ | #################### | +----------------------+ >>> print(' '*20 + 'end') end mysql> select space(20); +----------------------+ | space(20) | +----------------------+ | | +----------------------+ |
4. 字符串截取
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
>>> name = 'dkey' >>> print(%s) % name >>> name[0] 'd' >>> name[-1] 'y' >>> name[1] 'k' >>> name[1:4] 'key' >>> name[:] 'dkey' >>> name[1:4:2] 'ky' mysql> set @name:='dkey'; mysql> select left(@name,1); +---------------+ | left(@name,1) | +---------------+ | d | +---------------+ mysql> select right(@name,1); +----------------+ | right(@name,1) | +----------------+ | y | +----------------+ mysql> select substring(@name,2,3); +----------------------+ | substring(@name,2,3) | +----------------------+ | key | +----------------------+ mysql> select substring(@name,1); +--------------------+ | substring(@name,1) | +--------------------+ | dkey | +--------------------+ # 或者使用mid; mysql> select mid(@name,2,3); +----------------+ | mid(@name,2,3) | +----------------+ | dkey | +----------------+ mysql> select mid(@name,1); +--------------+ | mid(@name,1) | +--------------+ | dkey | +--------------+ |
5. 字符串格式化
1 2 |
>>> '{name},{alias}'.format(name='dkey',alias='ywnds') 'dkey,ywnds' |
6. 字符串长度
1 2 3 4 5 6 7 8 9 10 11 |
>>> name = "this is a test bar" >>> len(name) 18 mysql> set @name = "this is a test bar"; mysql> select length(@name); +---------------+ | length(@name) | +---------------+ | 18 | +---------------+ |
7. 字符串空格处理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
>>> s = ' abc ' >>> s.lstrip() 'abc ' >>> s.rstrip() ' abc' >>> s.strip() 'abc' mysql> set @s=' abc '; mysql> select ltrim(@s); +-----------+ | ltrim(@s) | +-----------+ | abc | +-----------+ mysql> select rtrim(@s); +-----------+ | rtrim(@s) | +-----------+ | abc | +-----------+ mysql> select trim(@s); +----------+ | trim(@s) | +----------+ | abc | +----------+ |
8. 字符串分隔
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
>>> 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 '; mysql> select replace(@s,' ',','); +---------------------+ | replace(@s,' ',',') | +---------------------+ | a,b,c,d,e, | +---------------------+ |
9. 字符串替换
1 2 3 4 5 6 7 8 9 10 11 |
>>> s = 'aabbcc' >>> s.replace('aa','tt') 'ttbbcc' mysql> set @s='aabbcc'; mysql> select replace(@s,'aa','tt'); +-----------------------+ | replace(@s,'aa','tt') | +-----------------------+ | ttbbcc | +-----------------------+ |
10. 字符串编码
1 2 3 4 5 |
>>> s = u'中文' >>> print(s.encode('utf-8')) 中文 mysql> select convert(@s using utf8); |
11. 判断字符串
1 2 3 4 5 6 7 8 9 10 11 |
>>> s = 'aabbcc' >>> s.startswith('aa') True mysql> set @s='aabbcc'; mysql> SELECT LOCATE('aa',@s,1); +-------------------+ | LOCATE('aa',@s,1) | +-------------------+ | 1 | +-------------------+ |
待续。。。