str_find_all_low_bit.py

class find_all_low(object):
    def __init__(self, string, wanted):
        self.forfind = string
        self.wanted = wanted
        self.appeareList = []
        
    def findonce(self):
        return self.forfind.find(self.wanted)
    
    def idx_sum(self,newidx):
        return self.appeareList[len(self.appeareList)-1] + newidx + len(self.wanted)
    
    def dofind(self):
        while True:
            appearance = self.findonce()
            if appearance >=0:
                self.forfind = self.forfind[appearance+len(self.wanted):]
                if not self.appeareList == []:
                    newidx = appearance
                    appearance = self.idx_sum(newidx)
                self.appeareList.append(appearance)               
            else:
                break
        return self.appeareList
        



string = 'ontheway 123 onmysid 234 no do on 345  on the land of honnor'
stringFind = find_all_low(string, '23')

resultList = stringFind.dofind()
print resultList

for re in range(0,len(resultList)):
    print '**'+string[resultList[re]:]+'\n'

python 里的str.find()只能找到最左边出现的第一个字符
这个方法通过反复执行find()方法,找到某字符在一个字符串中所有出现的低位index

结果:
[10, 21]
**23 onmysid 234 no do on 345  on the land of honnor


**234 no do on 345  on the land of honnor
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值