2017-02-21 16 views
5

W julia, Char i String nie są porównywalne.Jak przekonwertować Char do String w Julia?

julia> 'a' == "a" 
false 

Jak przekonwertować wartość Char na wartość typu String?

Próbowałem następujące funkcje, ale żaden z nich nie działa.

julia> convert(String, 'a') 
ERROR: MethodError: Cannot `convert` an object of type Char to an object of type String 

julia> String('a') 
ERROR: MethodError: Cannot `convert` an object of type Char to an object of type String 

julia> parse(String, 'a') 
ERROR: MethodError: no method matching parse(::Type{String}, ::Char) 

Odpowiedz

7

Sposób jest

string(c) 

np

julia> string('') 
"" 

Funkcja string działa obrócić wszystko w jej ciąg znaków, w taki sam sposób byłoby print ed. Rzeczywiście:

help?> string 
search: string String stringmime Cstring Cwstring RevString readstring 

    string(xs...) 

    Create a string from any values using the print function. 

    julia> string("a", 1, true) 
    "a1true" 
Powiązane problemy