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?
Get a substring of specified length from a string in Julia – SubString() Method. The SubString() is an inbuilt function in julia which is used to return a part of the specified parent string s within range i:j or r, where i, j and r is given as the parameter. Parameters: string: Specified parent string.
Splitting string into array of substrings in Julia – split() and rsplit() Method. The split() is an inbuilt function in julia which is used to split a specified string into an array of substrings on occurrences of the specified delimiter(s).
The replace() is an inbuilt function in julia that is used to replace a word or character with the specified string or character. Parameters: s::AbstractString: Specified string. pattern=>Word: Pattern is searched from the given sentence and then that pattern is replaced with the word.
Julia allows extracting elements of a string to form multiple substrings with the use of square brackets ( [ ]). Strings in Julia can be created using double quotes and triple quotes. Julia allows extracting characters by accessing a String with the use of Indexing.
String find is used to find the first occurrence of sub-string in the specified string being called upon. It returns the index of the first occurrence of the substring in the string from given starting position. The default value of starting position is 0.
Strings in Julia are a set of characters or a single character that is enclosed within double quotes (” “). It is a finite sequence of characters. Julia allows extracting elements of a string to form multiple substrings with the use of square brackets ( [ ]).
The indexOf () method is used to find an index of the specified substring in the present string. It returns a positive integer as an index if substring found else returns -1.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With