服务器维护,服务器代维,安全设置,漏洞扫描,入侵检测服务

运维之家

 找回密码
 注册
搜索
查看: 7305|回复: 0

Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小...

[复制链接]
dirtysea 发表于 2015-12-17 14:27:50 | 显示全部楼层 |阅读模式

去空格及特殊符号

http://pic002.cnblogs.com/images/2011/121863/2011010202035577.gif); font-family: 'Courier New' !important; font-size: 12px !important; background-position: 90% 0px; background-repeat: no-repeat no-repeat;">
s.strip().lstrip().rstrip(',')

复制字符串

http://pic002.cnblogs.com/images/2011/121863/2011010202035577.gif); font-family: 'Courier New' !important; font-size: 12px !important; background-position: 90% 0px; background-repeat: no-repeat no-repeat;">
#strcpy(sStr1,sStr2)
sStr1 = 'strcpy'
sStr2 = sStr1
sStr1 = 'strcpy2'
print sStr2

连接字符串

http://pic002.cnblogs.com/images/2011/121863/2011010202035577.gif); font-family: 'Courier New' !important; font-size: 12px !important; background-position: 90% 0px; background-repeat: no-repeat no-repeat;">
#strcat(sStr1,sStr2)
sStr1 = 'strcat'
sStr2 = 'append'
sStr1 += sStr2
print sStr1

查找字符

http://pic002.cnblogs.com/images/2011/121863/2011010202035577.gif); font-family: 'Courier New' !important; font-size: 12px !important; background-position: 90% 0px; background-repeat: no-repeat no-repeat;">
#strchr(sStr1,sStr2)
# < 0 为未找到
sStr1 = 'strchr'
sStr2 = 's'
nPos = sStr1.index(sStr2)
print nPos

比较字符串

http://pic002.cnblogs.com/images/2011/121863/2011010202035577.gif); font-family: 'Courier New' !important; font-size: 12px !important; background-position: 90% 0px; background-repeat: no-repeat no-repeat;">
#strcmp(sStr1,sStr2)
sStr1 = 'strchr'
sStr2 = 'strch'
print cmp(sStr1,sStr2)

扫描字符串是否包含指定的字符

http://pic002.cnblogs.com/images/2011/121863/2011010202035577.gif); font-family: 'Courier New' !important; font-size: 12px !important; background-position: 90% 0px; background-repeat: no-repeat no-repeat;">
#strspn(sStr1,sStr2)
sStr1 = '12345678'
sStr2 = '456'
#sStr1 and chars both in sStr1 and sStr2
print len(sStr1 and sStr2)

字符串长度

http://pic002.cnblogs.com/images/2011/121863/2011010202035577.gif); font-family: 'Courier New' !important; font-size: 12px !important; background-position: 90% 0px; background-repeat: no-repeat no-repeat;">
#strlen(sStr1)
sStr1 = 'strlen'
print len(sStr1)

将字符串中的大小写转换

http://pic002.cnblogs.com/images/2011/121863/2011010202035577.gif); font-family: 'Courier New' !important; font-size: 12px !important; background-position: 90% 0px; background-repeat: no-repeat no-repeat;">
#strlwr(sStr1)
sStr1 = 'JCstrlwr'
sStr1 = sStr1.upper()
#sStr1 = sStr1.lower()
print sStr1

追加指定长度的字符串

http://pic002.cnblogs.com/images/2011/121863/2011010202035577.gif); font-family: 'Courier New' !important; font-size: 12px !important; background-position: 90% 0px; background-repeat: no-repeat no-repeat;">
#strncat(sStr1,sStr2,n)
sStr1 = '12345'
sStr2 = 'abcdef'
n = 3
sStr1 += sStr2[0:n]
print sStr1

字符串指定长度比较

http://pic002.cnblogs.com/images/2011/121863/2011010202035577.gif); font-family: 'Courier New' !important; font-size: 12px !important; background-position: 90% 0px; background-repeat: no-repeat no-repeat;">
#strncmp(sStr1,sStr2,n)
sStr1 = '12345'
sStr2 = '123bc'
n = 3
print cmp(sStr1[0:n],sStr2[0:n])

复制指定长度的字符

http://pic002.cnblogs.com/images/2011/121863/2011010202035577.gif); font-family: 'Courier New' !important; font-size: 12px !important; background-position: 90% 0px; background-repeat: no-repeat no-repeat;">
#strncpy(sStr1,sStr2,n)
sStr1 = ''
sStr2 = '12345'
n = 3
sStr1 = sStr2[0:n]
print sStr1

将字符串前n个字符替换为指定的字符

http://pic002.cnblogs.com/images/2011/121863/2011010202035577.gif); font-family: 'Courier New' !important; font-size: 12px !important; background-position: 90% 0px; background-repeat: no-repeat no-repeat;">
#strnset(sStr1,ch,n)
sStr1 = '12345'
ch = 'r'
n = 3
sStr1 = n * ch + sStr1[3:]
print sStr1

扫描字符串

http://pic002.cnblogs.com/images/2011/121863/2011010202035577.gif); font-family: 'Courier New' !important; font-size: 12px !important; background-position: 90% 0px; background-repeat: no-repeat no-repeat;">
复制代码
#strpbrk(sStr1,sStr2)
sStr1 = 'cekjgdklab'
sStr2 = 'gka'
nPos = -1
for c in sStr1:
    if c in sStr2:
        nPos = sStr1.index(c)
        break
print nPos
复制代码

翻转字符串

http://pic002.cnblogs.com/images/2011/121863/2011010202035577.gif); font-family: 'Courier New' !important; font-size: 12px !important; background-position: 90% 0px; background-repeat: no-repeat no-repeat;">
#strrev(sStr1)
sStr1 = 'abcdefg'
sStr1 = sStr1[::-1]
print sStr1

查找字符串

http://pic002.cnblogs.com/images/2011/121863/2011010202035577.gif); font-family: 'Courier New' !important; font-size: 12px !important; background-position: 90% 0px; background-repeat: no-repeat no-repeat;">
#strstr(sStr1,sStr2)
sStr1 = 'abcdefg'
sStr2 = 'cde'
print sStr1.find(sStr2)

分割字符串

http://pic002.cnblogs.com/images/2011/121863/2011010202035577.gif); font-family: 'Courier New' !important; font-size: 12px !important; background-position: 90% 0px; background-repeat: no-repeat no-repeat;">
复制代码
#strtok(sStr1,sStr2)
sStr1 = 'ab,cde,fgh,ijk'
sStr2 = ','
sStr1 = sStr1[sStr1.find(sStr2) + 1:]
print sStr1
#或者
s = 'ab,cde,fgh,ijk'
print(s.split(','))
复制代码

连接字符串

http://pic002.cnblogs.com/images/2011/121863/2011010202035577.gif); font-family: 'Courier New' !important; font-size: 12px !important; background-position: 90% 0px; background-repeat: no-repeat no-repeat;">
delimiter = ','
mylist = ['Brazil', 'Russia', 'India', 'China']
print delimiter.join(mylist)

PHP 中 addslashes 的实现

http://pic002.cnblogs.com/images/2011/121863/2011010202035577.gif); font-family: 'Courier New' !important; font-size: 12px !important; background-position: 90% 0px; background-repeat: no-repeat no-repeat;">
复制代码
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)
复制代码

只显示字母与数字

http://pic002.cnblogs.com/images/2011/121863/2011010202035577.gif); font-family: 'Courier New' !important; font-size: 12px !important; background-position: 90% 0px; background-repeat: no-repeat no-repeat;">
复制代码
def OnlyCharNum(s,oth=''):
    s2 = s.lower();
    fomart = 'abcdefghijklmnopqrstuvwxyz0123456789'
    for c in s2:
        if not c in fomart:
            s = s.replace(c,'');
    return s;
 
print(OnlyStr("a000 aa-b"))
复制代码

 

截取字符串

复制代码
str = ’0123456789′
print str[0:3] #截取第一位到第三位的字符
print str[:] #截取字符串的全部字符
print str[6:] #截取第七个字符到结尾
print str[:-3] #截取从头开始到倒数第三个字符之前
print str[2] #截取第三个字符
print str[-1] #截取倒数第一个字符
print str[::-1] #创造一个与原字符串顺序相反的字符串
print str[-3:-1] #截取倒数第三位与倒数第一位之前的字符
print str[-3:] #截取倒数第三位到结尾
print str[:-5:-3] #逆序截取,具体啥意思没搞明白?
复制代码

 

 

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|手机版|Archiver|运维之家

GMT+8, 2024-3-19 17:08 , Processed in 0.139922 second(s), 17 queries .

Powered by Dirtysea

© 2008-2020 Dirtysea.com.

快速回复 返回顶部 返回列表