# Define the size of a String variable

**URL:** <https://discourse.julialang.org/t/define-the-size-of-a-string-variable/114474>\
**Category:** General Usage\
**Tags:** strings\
**Created:** [May 20, 2024, 12:13pm UTC](https://discourse.julialang.org/t/define-the-size-of-a-string-variable/114474 "2024-05-20T12:13:01Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ju\_ska](https://avatars.discourse-cdn.com/v4/letter/j/ecd19e/32.png) [@Ju\_ska](https://discourse.julialang.org/u/Ju_ska)\
**Post date:** [May 20, 2024, 12:13pm UTC](https://discourse.julialang.org/t/define-the-size-of-a-string-variable/114474/1 "2024-05-20T12:13:01Z")

</div>

Hi,

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

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

```

but with a fixed length of the strings inside.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 20, 2024, 12:28pm UTC](https://discourse.julialang.org/t/define-the-size-of-a-string-variable/114474/2 "2024-05-20T12:28:09Z")

</div>

For fixed width strings there’s

> **[GitHub - JuliaStrings/InlineStrings.jl: Fixed-width string types for Julia](https://github.com/JuliaStrings/InlineStrings.jl)**
>
> Fixed-width string types for Julia. Contribute to JuliaStrings/InlineStrings.jl development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [May 20, 2024, 12:29pm UTC](https://discourse.julialang.org/t/define-the-size-of-a-string-variable/114474/3 "2024-05-20T12:29:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [May 20, 2024, 12:42pm UTC](https://discourse.julialang.org/t/define-the-size-of-a-string-variable/114474/4 "2024-05-20T12:42:50Z")

</div>

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

```julia
# 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:

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

```

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

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

```

---

<div class="post-metadata">

**Author:** ![Ju\_ska](https://avatars.discourse-cdn.com/v4/letter/j/ecd19e/32.png) [@Ju\_ska](https://discourse.julialang.org/u/Ju_ska)\
**Post date:** [May 20, 2024, 1:20pm UTC](https://discourse.julialang.org/t/define-the-size-of-a-string-variable/114474/5 "2024-05-20T13:20:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [May 20, 2024, 1:51pm UTC](https://discourse.julialang.org/t/define-the-size-of-a-string-variable/114474/6 "2024-05-20T13:51:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [May 20, 2024, 2:46pm UTC](https://discourse.julialang.org/t/define-the-size-of-a-string-variable/114474/7 "2024-05-20T14:46:40Z")

</div>

Here is what StaticStrings.jl usage looks like:

```julia-repl
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-repl
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

```

---

<div class="post-metadata">

**Author:** ![Ju\_ska](https://avatars.discourse-cdn.com/v4/letter/j/ecd19e/32.png) [@Ju\_ska](https://discourse.julialang.org/u/Ju_ska)\
**Post date:** [May 20, 2024, 3:09pm UTC](https://discourse.julialang.org/t/define-the-size-of-a-string-variable/114474/8 "2024-05-20T15:09:47Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [May 20, 2024, 3:15pm UTC](https://discourse.julialang.org/t/define-the-size-of-a-string-variable/114474/9 "2024-05-20T15:15:02Z")

</div>

> [@Ju\_ska](#):
>
> ```julia
> 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-repl
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.

---

<div class="post-metadata">

**Author:** ![Ju\_ska](https://avatars.discourse-cdn.com/v4/letter/j/ecd19e/32.png) [@Ju\_ska](https://discourse.julialang.org/u/Ju_ska)\
**Post date:** [May 20, 2024, 3:52pm UTC](https://discourse.julialang.org/t/define-the-size-of-a-string-variable/114474/10 "2024-05-20T15:52:59Z")

</div>

🙂 well I’m just learning around…

---

<div class="post-metadata">

**Author:** ![Ju\_ska](https://avatars.discourse-cdn.com/v4/letter/j/ecd19e/32.png) [@Ju\_ska](https://discourse.julialang.org/u/Ju_ska)\
**Post date:** [May 21, 2024, 12:58pm UTC](https://discourse.julialang.org/t/define-the-size-of-a-string-variable/114474/12 "2024-05-21T12:58:43Z")

</div>

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