2017-02-21 55 views
5

在茱莉亞,Char和String不具有可比性。如何將Char轉換爲Julia中的字符串?

julia> 'a' == "a" 
false 

如何將Char值轉換爲String值?

我已經嘗試了以下功能,但都沒有工作。

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) 

回答

7

的方式是

string(c) 

例如

julia> string('') 
"" 

string功能的工作原理把任何東西放入它的字符串表示,以同樣的方式將是print版。確實

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"