我正在寻找一种方法来找到QStringList
中所有匹配子字符串的单元格位置。
列表以文本文件的形式填充,如下所示:
10:36:50,590/2002/1800
10:36:50,621/2002/1801
10:36:50,652/2002/1802
10:36:50,684/2002/1803
10:36:50,715/2002/1803
10:36:50,746/2002/1803
10:36:50,777/2002/1803/0/0/Target_Hit
10:36:50,809/2002/1802
10:36:50,840/2002/1802
10:36:50,871/2002/1802
10:36:50,965/2000/1831/0/0/Target_Hit
QStringList
的每个单元格包含一行文本文件。现在我想找到包含子字符串"Target_Hit"
的单元格的绝对点击次数和位置。
我试着像这样找到它的数量:int number_of_hits = List.indexOf(QRegExp(".*Target_Hit$));
,但返回一个-1
所以我猜QRegExp
是不正确的。
您不能为此使用indexOf,因为它只会返回一个索引,并且似乎您正在寻找多个。
我会逐行读取文件,检查每一行是否包含您正在寻找的内容,并注意到这一点,有效的伪代码:
initialise a counter to zero.
initialise a position list to an empty list.
read each line
check if the line contains the search string.
if it does: append the position into a list and increment the counter.
continue the next line.
那么,到最后,你就可以得到你想要的一切了。