Define the size of a String variable

Hi,

I don’t know if it’s possible to pre-allocate a Vector{String} with a fixed number of elements, like:

a=Array{String}(undef, 1, 4)

but with a fixed length of the strings inside.

For fixed width strings there’s

It’s not clear whether you want to fix the size of the array or the strings in it, there’s definitely no variable assigned to a string here.

StaticArrays.jl for fixing the size of arrays in the parametric types
InlineStrings.jl for a handful of string types with a fixed number of bytes (not necessarily the length of the string)
StaticStrings.jl seems to be like StaticArrays for strings, but I’ve never used it and don’t know if it’s inlineable.

For some use-cases, it might be sufficient to simply allocate a matrix of characters:

# number of strings
n_strings = 100
# number of characters per string
n_chars = 5

data = Array{Char}(undef, n_chars, n_strings)

which stores strings as vectors of characters in the matrix columns:

data[:, 1] .= ['a', 'b', 'c', 'd', 'e']

A vector of characters can be converted to a String like so:

String(@view data[:, 1]) # "abcde"

I would like to pre-allocate the array and stuff inside it.

You can preallocate an Array, you don’t need to fix the size in an array type for that. If the elements must also be allocated, you could just not make the instances yet, which is what undef is for. You just have to be careful to make an instance and assign it to an index before retrieving from that index.

You can’t preallocate a String because it’s immutable, so there’s no way to change it after you allocate one. InlineStrings.jl types don’t need to be (heap-)allocated because they have a fixed size in bytes.

Here is what StaticStrings.jl usage looks like:

julia> using StaticStrings

julia> a = Array{StaticString{32}}(undef, 1, 4);
1×4 Matrix{StaticString{32}}:
 
julia> a[1] = "Hello World"
"Hello World"

julia> a[1]
static"Hello World\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"32

julia> println(a[1])
Hello World

julia> a[2] = "Hola Mundo"
"Hola Mundo"

julia> a[2]
static"Hola Mundo\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"32

julia> vec(a)
4-element Vector{StaticString{32}}:
 static"Hello World\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"32
 static"Hola Mundo\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"32
 static"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"32
 static"\0\0\0\0\0\0\0\0\0\x19\xdf({\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"32

julia> a[3] = "Adios Mundo"
"Adios Mundo"

julia> a[4] = "Goodbye World"
"Goodbye World"

julia> vec(a)
4-element Vector{StaticString{32}}:
 static"Hello World\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"32
 static"Hola Mundo\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"32
 static"Adios Mundo\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"32
 static"Goodbye World\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"32

julia> Base.summarysize(a)
168

julia> Base.sizeof(a)
128

julia> 32*4
128

Here is InlineStrings:

julia> a = Array{String31}(undef, 1, 4)
1×4 Matrix{String31}:
 ""  ""  ""  ""

julia> a[1] = "Hello World"
"Hello World"

julia> a[2] = "Hola Mundo"
"Hola Mundo"

julia> a[3] = "Adios Mundo"
"Adios Mundo"

julia> a[4] = "Goodbye World"
"Goodbye World"

julia> vec(a)
4-element Vector{String31}:
 "Hello World"
 "Hola Mundo"
 "Adios Mundo"
 "Goodbye World"

julia> println(a[1])
Hello World

Ok thanks.
And how would I initialize an array of this kind of array ?
Something like:

a = Array{Array{String31}(undef, 1, 4)}(undef, 5, 1)

No, that does not work. The thing within the curly brackets, {} should just be the type.

julia> a = Array{Array{String31,2}}(undef, 5, 1)
5×1 Matrix{Matrix{String31}}:
 #undef
 #undef
 #undef
 #undef
 #undef

julia> a[1] = Array{String31}(undef, 1, 4)
1×4 Matrix{String31}:
 "\0\0\0\0\0\0\0\0\0\0\0\0\0C81[\e\rsgnirtSenilnI\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"  …  ""  ""  ""

julia> a[2] = String31["a" "b" "c" "d"] #note lack of commas
1×4 Matrix{String31}:
 "a"  "b"  "c"  "d"

Why are you making everything matrices though? This is not MATLAB.

1 Like

:slight_smile: well I’m just learning around…

Yup, I’ll do that, but I would like to make tests (no time today) before.