📌  相关文章
📜  Python|查找字符串中第 n 次出现的子字符串的方法

📅  最后修改于: 2022-05-13 01:54:32.020000             🧑  作者: Mango

Python|查找字符串中第 n 次出现的子字符串的方法

给定一个字符串和一个子字符串,编写一个Python程序来查找第 n出现的字符串。让我们讨论一些解决给定任务的方法。

方法 #1:使用 re

# Python code to demonstrate 
# to find nth occurrence of substring
  
import re
  
# Initialising values
ini_str = "abababababab"
substr = "ab"
occurrence = 4
  
  
# Finding nth occurrence of substring
inilist = [m.start() for m in re.finditer(r"ab", ini_str)]
if len(inilist)>= 4:
      
    # Printing result
    print ("Nth occurrence of substring at", inilist[occurrence-1])
else:
    print ("No {} occurrence of substring lies in given string".format(occurrence))


方法 #2:使用 find() 方法

# Python code to demonstrate 
# to find nth occurrence of substring
  
# Initialising values
ini_str = "abababababab"
sub_str = "ab"
occurrence = 4
  
  
# Finding nth occurrence of substring
val = -1
for i in range(0, occurrence):
    val = ini_str.find(sub_str, val + 1)
       
# Printing nth occurrence
print ("Nth occurrence is at", val)


方法 #3:使用 startswith() 和列表推导

# Python code to demonstrate 
# to find nth occurrence of substring
  
# Initialising values
ini_str = "abababababab"
substr = "ab"
occurrence = 4
  
  
# Finding nth occurrence of substring
inilist = [i for i in range(0, len(ini_str))
            if ini_str[i:].startswith(substr)]
  
if len(inilist)>= 4:
      
    # Printing result
    print ("Nth occurrence of substring at", inilist[occurrence-1])
else:
    print ("No {} occurrence of substring lies in given string".format(occurrence))