A couple of questions about arrays and "findall" function

Hello there !! I am new to programming in general and I have a couple of questions about how to use strings, if I have an example text and I would like to count the spaces in the text like this:

ExampleText= "a b c d e f g k k k"
count(i->(i==' '), ExampleText)

And it works!!!,
But researching the documentation I found the function “findall” my first question is whether
Can I count spaces using this function?
And the second question is if I want to make an array that contains as elements each of the words of the text. That is:
text [1] = a, text [2] = b, … text [end] = k.

Yes, but using count will be faster. BTW, shouldn’t your code say count(i->(i==' '), ExampleText)? Perhaps a slightly more elegant call would be

count(isspace, ExampleText)

where detects various whitespace.

For your second question, use split:

jl> split(ExampleText)
10-element Vector{SubString{String}}:
 "a"
 "b"
 "c"
 "d"
 "e"
 "f"
 "g"
 "k"
 "k"
 "k"

Another alternative to isspace and i->(i==' ') is ==(' ').

2 Likes

In Julia 1.7 you can use:

count(' ', string)

The same for findall.

4 Likes

Thank you for everything! :smiley:

1 Like