Python指令碼語言,

來源:互聯網
上載者:User

Python指令碼語言,

Python 字串操作(string替換、刪除、截取、複製、串連、比較、尋找、包含、大小寫轉換、分割等) 

字串長度  
#strlen(sStr1)   sStr= 'strlen'   print len(sStr1)

連接字串 

#strcat(sStr1,sStr)   sStr= 'strcat'   sStr = 'append'   sStr+= sStr   print sStr
比較字串  
#strcmp(sStr1,sStr)   sStr= 'strchr'   sStr = 'strch'   print cmp(sStr1,sStr)
去空格及特殊符號  
s.strip() s.lstrip() s.rstrip(',') 
複製字串  
#strcpy(sStr1,sStr)   sStr= 'strcpy'   sStr = sStr  sStr= 'strcpy'   print sStr  

尋找字元  

#strchr(sStr1,sStr)   sStr= 'strchr'   sStr = 's'   nPos = sStr1.index(sStr)print nPos
掃描字串是否包含指定的字元  
#strspn(sStr1,sStr)   sStr= '1345678'   sStr = '456'   #sStrand chars both in sStrand sStr   print len(sStrand sStr)
將字串中的大小寫轉換  
#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 
將字串前n個字元替換為指定的字元  
#strnset(sStr1,ch,n)   sStr= '1345'   ch = 'r'   n = 3 sStr= n * ch + sStr1[3:]   print sStr
翻轉字串  
#strrev(sStr1)   sStr= 'abcdefg'   sStr= sStr1[::-1]   print sStr
尋找字串  
#strstr(sStr1,sStr)   sStr= 'abcdefg'   sStr = 'cde'   print sStr1.find(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 
分割字串  
#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")) 




查看評論

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.