获取子串位置

# coding:utf-8


# 获取s中的子串(从第i个位置获取和t一样长的子串)
def substr(s,i,m):
    return s[i:i+m]


def index(s,t,pos):
    if pos >= 0:
        n = len(s)
        m = len(t)
        i = pos
        while i <= n-m+1:
           s1 = substr(s,i,m)
           if cmp(s1,t):
               i += 1
           else:
               print i+1
               i += 1
    else:
        print 'not exist'


index('I love China! Which country do you love best?','love',0)

猜你喜欢

转载自blog.csdn.net/fredinators/article/details/79524849