获取字符串第n个出现的索引?(Get the index of the nth occurrence of a string?)
除非我缺少一个明显的内置方法,否则在字符串中第一次出现字符串的最快方式是什么?
我意识到,我可以通过在循环的每次迭代更新其起始索引来循环IndexOf方法。 但是这样做对我来说似乎是浪费的。
Unless I am missing an obvious built-in method, what is the quickest way to get the nth occurrence of a string within a string?
I realize that I could loop the IndexOf method by updating its start index on each iteration of the loop. But doing it this way seems wasteful to me.
最满意答案
这基本上是你需要做的 - 或者至少这是最简单的解决方案。 所有你会“浪费”是n方法调用的成本 - 如果你考虑,你实际上不会检查任何情况两次。 (IndexOf会在找到匹配项后立即返回,并且您将继续停留在该位置。)
That's basically what you need to do - or at least, it's the easiest solution. All you'd be "wasting" is the cost of n method invocations - you won't actually be checking any case twice, if you think about it. (IndexOf will return as soon as it finds the match, and you'll keep going from where it left off.)
相关问答
更多-
>>> n = 2 >>> groups = text.split('_') >>> '_'.join(groups[:n]), '_'.join(groups[n:]) ('20_231', 'myString_234') 似乎这是最可读的方式,替代方案是正则表达式) >>> n = 2 >>> groups = text.split('_') >>> '_'.join(groups[:n]), '_'.join(groups[n:]) ('20_231', 'myString_234') Seems ...
-
字符串的第n个索引(Index of nth Occurrence of the string)[2023-01-07]
Boost中有一个find_nth模板函数: http : //www.boost.org/doc/libs/1_54_0/doc/html/boost/algorithm/find_nth.html #include#include using namespace std; using namespace boost; int main() { string a = "The rain in Spai ... -
这基本上是你需要做的 - 或者至少这是最简单的解决方案。 所有你会“浪费”是n方法调用的成本 - 如果你考虑,你实际上不会检查任何情况两次。 (IndexOf会在找到匹配项后立即返回,并且您将继续停留在该位置。) That's basically what you need to do - or at least, it's the easiest solution. All you'd be "wasting" is the cost of n method invocations - you won't ...
-
function getPosition(string, subString, index) { return string.split(subString, index).join(subString).length; } function getPosition(string, subString, index) { return string.split(subString, index).join(subString).length; }
-
public int GetNthIndex(string s, char t, int n) { int count = 0; for (int i = 0; i < s.Length; i++) { if (s[i] == t) { count++; if (count == n) { return i; } ...
-
您要查找的方法是charAt 。 这里有一个例子: String text = "foo"; char a_char = text.charAt(0); System.out.println( a_char ); // Prints f 有关更多信息,请参阅String.charAt上的Java文档 。 如果你想要另一个简单的教程, 这一个或这个 。 如果不希望将结果作为char数据类型,而是将其作为字符串,则可以使用Character.toString方法: String text = "foo"; S ...
-
您可以使用{}模式重复: /(?:"([^"]*)";){3}/ # first match group will be 'Opis dogodka' 演示 或者,使用全局类型匹配,然后进行第三次匹配。 这可能需要切片或循环等逻辑: /"([^"]*)";/g 演示2 或者,只需手动输入前两个模式即可跳过: /^"[^"]*";"[^"]*";("[^"]*";)/ 演示3 或者组合重复跳过第一个n-1然后捕获第n个匹配: /^(?:"[^"]*";){2}("[^"]*";)/ 演示4 Yo ...
-
根据需要调整7和my_pattern 。 awk -v N=7 '{print}/my_pattern/&&--N<=0{exit}' 更隐秘的是,以下内容也适用: awk -v N=7 '1;/my_pattern/&&--N<=0{exit}' 上述两个实际上都停止在包含模式的第N行,而不是模式的第N次出现。 如果您想要第N次出现的模式: awk -v N=7 -v M=my_pattern '1;(N-=gsub(M,""))<=0{exit}' 例如: printf %s\\n line1 " ...
-
现在我有: SELECT substring_index( substr(col, locate('"id":"', col, locate('"id":"', col) + 6) + 6), '"', 1) FROM table 很想看到一个“更好”的答案:-) For now I have: SELECT substring_index( substr(col, locate('"id":"', col, locate('"id" ...
-
cut可以做到: $ cut -d'/' -f5- <<< "$s" 0.0. Loading_SOR_to_Landing/EGI/EGI_WV_WELLHDR.pjb 它将分隔符设置为/并从第5个字段打印到结尾( 5- )。 cut can make it: $ cut -d'/' -f5- <<< "$s" 0.0. Loading_SOR_to_Landing/EGI/EGI_WV_WELLHDR.pjb It sets the delimiter as / and prints from 5t ...