关于字符串的常见操作,请参见如下示例:
# @Time : 2017/7/2 21:26
# @Author : 原创作者:谷哥的小弟
# @Site : 博客地址:http://blog.csdn.net/lfdfhl
# @DESC : String常见操作
string='hello,my name is hanmeimei'
'''
find()
判断子串是否在String中,若存在则返回子串在String中开始的索引值,否则返回-1
rfind()类似于find()函数,不过是从右边开始查找.
'''
index1=string.find('llo')
print('index1=',index1)
#利用第二个,第三个参数指定判断的范围
index2=string.find('llo',0,10)
print('index2=',index2)
index3=string.find('llo',7,13)
print('index3=',index3)
print('= '*15)
'''
count()
统计子串在String中出现的次数
'''
count1=string.count('m')
#利用第二个,第三个参数指定统计的范围
count2=string.count('m',0,8)
print('count1=',count1)
print('count2=',count2)
print('= '*15)
'''
replace()
把string中的str1替换成str2,如果count指定,则替换次数不超过count
'''
replaceResult1=string.replace('mei','XX')
print('replaceResult1=',replaceResult1)
replaceResult2=string.replace('mei','XX',1)
print('replaceResult2=',replaceResult2)
print('string=',string)
print('= '*15)
'''
split()
以str为分隔符切割string,可用maxsplit指定分割次数,即分隔成 maxsplit + 1 个子字符串
'''
fragment1=string.split(' ')
print('fragment1=',fragment1)
fragment2=string.split(' ',2)
print('fragment2=',fragment2)
print('= '*15)
'''
startswith()
判断string是否以str开头
'''
result1=string.startswith('hello')
result2=string.startswith('ello')
print('result1=',result1)
print('result2=',result2)
print('= '*15)
'''
endsWith()
判断string是否以str结尾
'''
result3=string.endswith('mei')
result4=string.endswith('xxx')
print('result3=',result3)
print('result4=',result4)
print('= '*15)
'''
upper()
将string中字母变成大写
'''
upperString=string.upper()
print('upperString=',upperString)
print('= '*15)
'''
lower()
将string中字母变成小写
'''
lowerString=upperString.lower()
print('lowerString=',lowerString)
print('= '*15)
'''
strip()
删除字符串两端的空格。
类似的方法有lstrip(),rstrip()删除字符串左侧,右侧的空格
'''
name=' Hello all '
stripResult=name.strip()
print('stripResult=',stripResult)
print('= '*15)
'''
partition()
把string以str分割成三部分,str前,str和str后
rpartition()类似于 partition()函数,不过是从右边开始.
'''
fragments=string.partition('llo')
print('fragments=',fragments)
print('= '*15)
'''
splitlines()
按照行分隔即\n,返回一个包含各行作为元素的列表
'''
lines="good\nmorning\nsir"
resultLines=lines.splitlines()
print('resultLines=',resultLines)
print('= '*15)
'''
isalpha()
判断string中是否全为字母
'''
isalpha=string.isalpha()
print('isalpha=',isalpha)
print('= '*15)
'''
isdigit()
判断string中是否全为数字
'''
digitString='123456789'
isdigit=digitString.isdigit()
print('isdigit=',isdigit)
print('= '*15)
'''
join()
str.join(sequence)将字符串、元组、列表中的元素以指定的字符(str)连接生成一个新的字符串
'''
str='_'
sequence=['Hello','Everyone','Thanks']
joinResult=str.join(sequence)
print('joinResult=',joinResult)
print('= '*15)
测试结果如下:
index1= 2
index2= 2
index3= -1
= = = = = = = = = = = = = = =
count1= 4
count2= 1
= = = = = = = = = = = = = = =
replaceResult1= hello,my name is hanXXXX
replaceResult2= hello,my name is hanXXmei
string= hello,my name is hanmeimei
= = = = = = = = = = = = = = =
fragment1= ['hello,my', 'name', 'is', 'hanmeimei']
fragment2= ['hello,my', 'name', 'is hanmeimei']
= = = = = = = = = = = = = = =
result1= True
result2= False
= = = = = = = = = = = = = = =
result3= True
result4= False
= = = = = = = = = = = = = = =
upperString= HELLO,MY NAME IS HANMEIMEI
= = = = = = = = = = = = = = =
lowerString= hello,my name is hanmeimei
= = = = = = = = = = = = = = =
stripResult= Hello all
= = = = = = = = = = = = = = =
fragments= ('he', 'llo', ',my name is hanmeimei')
= = = = = = = = = = = = = = =
resultLines= ['good', 'morning', 'sir']
= = = = = = = = = = = = = = =
isalpha= False
= = = = = = = = = = = = = = =
isdigit= True
= = = = = = = = = = = = = = =
joinResult= Hello_Everyone_Thanks
= = = = = = = = = = = = = = =