user1748681
user1748681

Reputation: 1247

Julia Int Value of a String

I'm fairly new to the Julia language and am struggling to find the integer value of a string.

I know that calling int('a') will return the value I'm looking for, but I cannot figure out how to do the same for int("a").

Is there a way to convert a string value to a character?

UPDATE: Yes, the solution you provided does work, but not in my case. I probably should have been more specific. Here is what my array of strings looks like

array = ["12", "13", "14"] ["16", "A"]

array[2][2] returns "A" not 'A'

Upvotes: 5

Views: 2075

Answers (2)

tholy
tholy

Reputation: 12179

Strings are represented internally as an array of Uint8, so for ASCIIStrings the following works:

julia> "Hello".data
5-element Array{Uint8,1}:
 0x48
 0x65
 0x6c
 0x6c
 0x6f

The definition of a character is more complicated for Unicode, however, so use this with caution.

Upvotes: 4

ruakh
ruakh

Reputation: 183241

From the "String Basics" section of the Julia manual:

julia> str = "Hello, world.\n"
"Hello, world.\n"

If you want to extract a character from a string, you index into it:

julia> str[1]
'H'

julia> str[6]
','

julia> str[end]
'\n'

So you can get the character at index 1, and then pass that to int.

Upvotes: 2

Related Questions