问题:
给出一个字符串,其中字母h至少出现两次。 从该字符串中删除第一个和最后一个匹配项 字母h以及它们之间的所有字符。
如何找到h的第一个和最后一个出现?以及如何删除它们以及它们之间的字符?
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
答案 0 :(得分:1)
您需要使用正则表达式:
>>> import re
>>> s = 'jusht exhamplhe'
>>> re.sub(r'h.+h', '', s)
'juse'
答案 1 :(得分:1)
答案 2 :(得分:1)
如何找到h的第一个和最后一个出现?
首次出现:
first_h_index=origin_s.find("h");
最后一次发生:
last_h_index=origin_s.rfind("h");
如何删除它们以及它们之间的字符?
答案 3 :(得分:0)
string = '1234-123456789'
char_list = []
for i in string:
char_list.append(string[i])
char_list.remove('character_to_remove')
根据文档,remove(arg)
是一种作用于可变迭代器(例如list
)的方法,该方法删除了迭代器中arg
的第一个实例。
答案 4 :(得分:0)
这将帮助您更清楚地了解:
string = 'abchdef$ghi'
first=string.find('h')
last=string.rfind('h')
res=string[:first]+string[last+1:]
print(res)