When to use String() over string()?

Hi this may be a silly question but just wondering if there are cases when String() be used over string()? To the untutored, it appears that they are doing the same thing.

string(SubString("use string")) === String(SubString("use string"))
true

Except that string() also has a method for integer values. I didn’t grasp other distinctions between the two functions from the docs. But I often do clumsy things with Julia functions, (e.g. using Joins on Floating Point Values) before being corrected here. Any clarifications would be appreciated. Thanks!

I would use String() when you want to guarantee that what comes out is a standard string object.

For instance, if we were using types from InlineStrings, then we might want to eventually convert them to standard strings if we want to make containers with strings of other types. As shown below, String() will output a String while string (lowercase) will return the original type.

julia> using InlineStrings
julia> s = String7("asdf")
"asdf"

julia> typeof(s)
String7

julia> typeof(String(s))
String

julia> typeof(string(s))
String7

1 Like

One important distinction is that string([‘a’, ‘b’, ‘c’]) is “[‘a’, ‘b’, ‘c’]” but String([‘a’, ‘b’, ‘c’]) is “abc”.

6 Likes

Thanks so much for the clarification! In general is there a place to look up such distinctions if one lacks the background to extrapolate the details from the docs? For example if I have Vector of Strings

string(["a","b","c"])
"[\"a\", \"b\", \"c\"]"

whereas String(["a","b","c"]) without broadcasting returns a method error. But I wouldn’t have known this without the instructive Vector of Characters example provided above or just trial and error.

Also why does

julia> string.(["a","b","c"]) === String.(["a","b","c"])
false

whereas the earlier example

string(SubString("use string")) === String(SubString("use string"))
true

I assume this has to do with broadcasting and where the object is stored? Thanks again!

distinction is just that String is a type, string can be named something else like stringify or make_string_for_human_to_read.

When you call the type String, think of it as doing convert(String, x), and the mental model is if you’re representing the (logically) same data, you can do convert(), if you’re just getting the human-readable string representation of something, then you’re doing string().

my go to example

julia> String([0x39, 0x52])
"9R"

julia> string([0x39, 0x52])
"UInt8[0x39, 0x52]"
9 Likes

Thanks! This was really informative. I think I understand the docs a little better now that you pointed that out. So roughly speaking:

String(stuff) is equivalent to convert(String, stuff)

whereas

string(stuff) is equivalent to convert output of print(stuff) into type String.

Except that when it can’t convert print(stuff) e.g. the case above of a Vector of Strings

print(["a","b","c"])
["a", "b", "c"]

where "["a","b","c"]" would create a syntax error of juxtaposing a string literal then something like

string(["a","b","c"])
"[\"a\", \"b\", \"c\"]"

is done?

so "[\"a\", \"b\", \"c\"]" is precisely how you have to type a literal string, e.g.

println("[\"a\", \"b\", \"c\"]")
["a", "b", "c"]
1 Like
String([0x61,0x62,0x63]) =="abc"
1 Like