Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 683
Luis.Alberto Avatar asked May 08 '21 07:05

Luis.Alberto


People also ask

How do I get a substring in Julia?

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.

How do you split strings in Julia?

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).

How do you replace characters in strings in Julia?

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.

How do you extract a string from a string in Julia?

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.

How do you find the first occurrence of a substring?

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.

What is a single string in Julia?

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 ( [ ]).

How to find the index of a substring in a string?

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.


Video Answer


1 Answers

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.

like image 143
logankilpatrick Avatar answered Oct 21 '22 15:10

logankilpatrick