Python字串搜尋並返回子字串位置

2021-06-07 22:58:02 字數 1262 閱讀 9026

# multiple searches of a string for a substring

# using s.find(sub[ ,start[, end]])

text='msksaspkepeqlrklfigglsfettdeslrsahfesssygsagrrf'

search='sa'

start=0

whiletrue:

index=text.find(search, start)

# if search string not found, find() returns -1

# search is complete, break out of the while loop

ifindex==-1:

break

print("%s found at index %d"%(search, index) )

# move to next possible start position

start=index+1

//結果:

sa found at index 3

sa found at index 31

sa found at index 41

**:

搜尋 列舉字串子串問題

求字串的子串問題有兩類,一種是不連續,一種是連續。然而這兩種都可以用暴力求解,不過容易 time limited exceed 那可以只用兩個for迴圈就get全部子串嗎?求不連續的子串 用dfs 深度優先搜尋 這裡看乙個題 hd 1015 safecracker 這個題大意就是 在給出的5 12個...

python字串擷取子串

在python中沒有類似sub 或者substring 的方法,但是字串的擷取操作卻是更加簡單。只需要把字串看作是乙個字元陣列,擷取子串非常方便。多餘的話就不囉嗦了,看下面的例子就明白了。str 0123456789 print str 0 3 擷取第一位到第三位的字元 print str 擷取字串...

C語言實現字串匹配並返回匹配字串

最近在寫乙個程式,需要用到字串匹配,並且返回匹配的字串,c語言庫函式中的strtstr無法滿足我的要求,只能自己寫了。如下 string match function char matchstring const char buf,const char sub 在匹配過程中發現有乙個字元和子串中的不...