From array of Char to String

227 views
Skip to first unread message

Ning Yin

unread,
Aug 19, 2014, 8:04:29 AM8/19/14
to julia...@googlegroups.com
Hi Julia users, 

I was trying to convert an array of char's to string, and I've noticed functions such as string or CharString, but non of these seem to work for my case:

Julia console:
==============
julia> a = ['a', 'b', 'c']
3-element Array{Char,1}:
 'a'
 'b'
 'c'

julia> string(a)
"Char[a,b,c]"

julia> CharString(a)
ERROR: ArgumentError("UTF32String data must be NULL-terminated")
===============

Any idea? I'm using Julia Version 0.4.0-dev+238 (just cloned from github). 

Thanks!
-Ning

Jacob Quinn

unread,
Aug 19, 2014, 9:50:33 AM8/19/14
to julia...@googlegroups.com
Try join()

Ning Yin

unread,
Aug 19, 2014, 11:55:16 PM8/19/14
to julia...@googlegroups.com, quinn....@gmail.com
Thanks! That worked. 

I also found out that both string(a...) and CharString(a...) worked, though I don't understand why the '...' is necessary here.

Isaiah Norton

unread,
Aug 20, 2014, 12:11:02 AM8/20/14
to julia...@googlegroups.com
That is the splice operator (also known as "splat"). It causes the contents of `a` to be passed as indvidual arguments rather than passing `a` as an array. So, string() behaves differently.

John Myles White

unread,
Aug 20, 2014, 12:12:37 AM8/20/14
to julia...@googlegroups.com
We should really have a FAQ for this. This is something that tripped me up for a while.

I’ll write up sometime.

 — John

Ivar Nesje

unread,
Aug 20, 2014, 2:46:05 AM8/20/14
to julia...@googlegroups.com
It also seems kind of reasonable to special case string() for Array{Char,1}. It will be inconsistent with other Array stringifications, but print/show/writemime/dump/xdump/showcompact/... can still give emphasize the array property with an array representation.

Steven G. Johnson

unread,
Aug 20, 2014, 10:53:08 AM8/20/14
to julia...@googlegroups.com


On Tuesday, August 19, 2014 8:04:29 AM UTC-4, Ning Yin wrote:
I was trying to convert an array of char's to string, and I've noticed functions such as string or CharString, but non of these seem to work for my case:

Julia console:
==============
julia> a = ['a', 'b', 'c']

CharString is deprecated in Julia 0.3.   You can, however, do:

     utf32(a)

on an array of Char.  See the documentation for utf32 in the manual.
Reply all
Reply to author
Forward
0 new messages