跳至内容
Python 俱乐部
用户工具
登录
站点工具
搜索
工具
显示页面
修订记录
反向链接
最近更改
媒体管理器
网站地图
登录
>
最近更改
媒体管理器
网站地图
您的足迹:
python-basic:string
本页面只读。您可以查看源文件,但不能更改它。如果您觉得这是系统错误,请联系管理员。
====== Python字符串操作 ====== [[python-basic:string-number|python如何判断一个字符串只包含数字字符]] \\ [[python-basic:string-compare|python 字符串比较]] \\ 下面列出了常用的python实现的字符串操作 ===== 1.复制字符串 ===== <code python> #strcpy(sStr1,sStr2) sStr1 = 'strcpy' sStr2 = sStr1 sStr1 = 'strcpy2' print sStr2 </code> ===== 2.连接字符串 ===== <code python> #strcat(sStr1,sStr2) sStr1 = 'strcat' sStr2 = 'append' sStr1 += sStr2 print sStr1 </code> ===== 3.查找字符 ===== <code python> #strchr(sStr1,sStr2) sStr1 = 'strchr' sStr2 = 'r' nPos = sStr1.index(sStr2) print nPos </code> ===== 4.比较字符串 ===== <code python> #strcmp(sStr1,sStr2) sStr1 = 'strchr' sStr2 = 'strch' print cmp(sStr1,sStr2) </code> ===== 5.扫描字符串是否包含指定的字符 ===== <code python> #strspn(sStr1,sStr2) sStr1 = '12345678' sStr2 = '456' #sStr1 and chars both in sStr1 and sStr2 print len(sStr1 and sStr2) </code> ===== 6.字符串长度 ===== <code python> #strlen(sStr1) sStr1 = 'strlen' print len(sStr1) </code> ===== 7.将字符串中的小写字符转换为大写字符 ===== <code python> #strlwr(sStr1) sStr1 = 'JCstrlwr' sStr1 = sStr1.upper() print sStr1 </code> ===== 8.追加指定长度的字符串 ===== <code python> #strncat(sStr1,sStr2,n) sStr1 = '12345' sStr2 = 'abcdef' n = 3 sStr1 += sStr2[0:n] print sStr1 </code> ===== 9.字符串指定长度比较 ===== <code python> #strncmp(sStr1,sStr2,n) sStr1 = '12345' sStr2 = '123bc' n = 3 print cmp(sStr1[0:n],sStr2[0:n]) </code> ===== 10.复制指定长度的字符 ===== <code python> #strncpy(sStr1,sStr2,n) sStr1 = '' sStr2 = '12345' n = 3 sStr1 = sStr2[0:n] print sStr1 </code> ===== 11.字符串比较,不区分大小写 ===== <code python> #stricmp(sStr1,sStr2) sStr1 = 'abcefg' sStr2 = 'ABCEFG' print cmp(sStr1.upper(),sStr2.upper()) </code> ===== 12.将字符串前n个字符替换为指定的字符 ===== <code python> #strnset(sStr1,ch,n) sStr1 = '12345' ch = 'r' n = 3 sStr1 = n * ch + sStr1[3:] print sStr1 </code> ===== 13.扫描字符串===== <code python> #strpbrk(sStr1,sStr2) sStr1 = 'cekjgdklab' sStr2 = 'gka' nPos = -1 for c in sStr1: if c in sStr2: nPos = sStr1.index(c) break print nPos </code> ===== 14.翻转字符串 ===== <code python> #strrev(sStr1) sStr1 = 'abcdefg' sStr1 = sStr1[::-1] print sStr1 </code> ===== 15.查找字符串 ===== [[python-basic:string-strstr|python strstr]] <code python> #strstr(sStr1,sStr2) sStr1 = 'abcdefg' sStr2 = 'cde' print sStr1.find(sStr2) </code> ===== 16.分割字符串 ===== <code python> #strtok(sStr1,sStr2) sStr1 = 'ab,cde,fgh,ijk' sStr2 = ',' sStr1 = sStr1[sStr1.find(sStr2) + 1:] print sStr1 </code>
python-basic/string.txt
· 最后更改: 2010/06/02 01:18 (外部编辑)
页面工具
显示页面
修订记录
反向链接
回到顶部