简体   繁体   English

如何在 Julia 中的字符串数组中查找 substring 或字符

[英]How to find a substring or a char in an array of strings in Julia

Whats a more efficient way to do the same thing that the next line of code is doing:有什么更有效的方法来做下一行代码正在做的事情:

X = ["Please", "Thx", "Hello", "World"]
findfirst(k->occursin('H',k)==true,X)

So basically i'm trying to find the first element of the array X that has the uppercase letter H, so in the example the output is 3, but is there a more efficient way of doing this?所以基本上我试图找到数组 X 的第一个元素是大写字母 H,所以在这个例子中 output 是 3,但是有没有更有效的方法呢?

Unless I am missing something fundamental here, check out the timings below:除非我在这里遗漏了一些基本的东西,否则请查看以下时间:


julia> @time findfirst(k->occursin('H',k),X)
  0.015461 seconds (11.20 k allocations: 689.455 KiB, 99.63% compilation time)
3

julia> @time findfirst(contains('H'),X)
  0.000008 seconds
3

Seems like the contains route is significantly more performant than occursin .似乎contains路线的性能明显优于发生的occursin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM