Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等) s.strip() .lstrip() .rstrip(',') #strcpy(sStr1,sStr) sStr= 'strcpy' sStr = sStr sStr= 'strcpy' print sStr #strcat(sStr1,sStr) sStr= 'strcat' sStr = 'append' print sStr #strchr(sStr1,sStr) sStr= 'strchr' sStr = 's' nPos = sStr1.index(sStr) print nPos #strcmp(sStr1,sStr) sStr= 'strchr' sStr = 'strch' print cmp(sStr1,sStr) #strspn(sStr1,sStr) sStr= '1345678' sStr = '456' #sStrand chars both in sStrand sStr print len(sStrand sStr) #strlen(sStr1) sStr= 'strlen' print len(sStr1) #strlwr(sStr1) sStr= 'JCstrlwr' sStr= sStr1.upper() #sStr= sStr1.lower() print sStr #strncat(sStr1,sStr,n) sStr= '1345' sStr = 'abcdef' n = 3 sStr+= sStr[0:n] print sStr #strncmp(sStr1,sStr,n) sStr= '1345' sStr = '13bc' n = 3 print cmp(sStr1[0:n],sStr[0:n]) #strncpy(sStr1,sStr,n) sStr= '' sStr = '1345' n = 3 sStr= sStr[0:n] print sStr #strnset(sStr1,ch,n) sStr= '1345' ch = 'r' n = 3 sStr= n * ch + sStr1[3:] print sStr #strpbrk(sStr1,sStr) sStr= 'cekjgdklab' sStr = 'gka' nPos = -1 for c in sStr1: if c in sStr: nPos = sStr1.index(c) break print nPos #strrev(sStr1) sStr= 'abcdefg' sStr= sStr1[::-1] print sStr #strstr(sStr1,sStr) sStr= 'abcdefg' sStr = 'cde' print sStr1.find(sStr) #strtok(sStr1,sStr) sStr= 'ab,cde,fgh,ijk' sStr = ',' sStr= sStr1[sStr1.find(sStr) + 1:] print sStr 或者 s = 'ab,cde,fgh,ijk' print(s.split(',')) delimiter = ',' mylist = ['Brazil', 'Russia', 'India', 'China'] print delimiter.join(mylist) PHP 中 addslashes 的实现 def addslashes(s): d = {'"':'\\"', "'":"\\'", "\0":"\\\0", "\\":"\\\\"} return ''.join(d.get(c, c) for c in s) s = "John 'Johny' Doe (a.k.a. \"Super Joe\")\\\0" print s print addslashes(s) def OnlyCharNum(s,oth=''): s = s.lower(); fomart = 'abcdefghijklmnopqrstuvwxyz013456789' for c in s: if not c in fomart: s = s.replace(c,''); return s; print(OnlyStr("a000 aa-b"))
本文转自 chengxuyonghu 51CTO博客,原文链接:http://blog.51cto.com/6226001001/1548709,如需转载请自行联系原作者