# A question about SubString

**URL:** <https://discourse.julialang.org/t/a-question-about-substring/123838>\
**Category:** New to Julia\
**Tags:** strings, indexing, views\
**Created:** [December 14, 2024, 9:52am UTC](https://discourse.julialang.org/t/a-question-about-substring/123838 "2024-12-14T09:52:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [December 14, 2024, 9:52am UTC](https://discourse.julialang.org/t/a-question-about-substring/123838/1 "2024-12-14T09:52:06Z")

</div>

Hello,  
What is the difference between SubString and Indexing? I did:

```julia
julia> str = "Where the mind is without fear"
"Where the mind is without fear"

julia> SubString(str, 1, 5)
"Where"

julia> str[1:5]
"Where"

```

The results of both are the same. When should I use SubString?

Thank you.

---

<div class="post-metadata">

**Author:** ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)\
**Post date:** [December 14, 2024, 10:34am UTC](https://discourse.julialang.org/t/a-question-about-substring/123838/2 "2024-12-14T10:34:53Z")

</div>

Creating a `String` requires a new allocation on the heap, where the string stores a full copy of its data. A `SubString`, because it refers to an existing `String`, only needs to store references, and therefore requires no heap allocation to create.  
A `SubString` is essentially a pointer and two integers.

---

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [December 14, 2024, 10:50am UTC](https://discourse.julialang.org/t/a-question-about-substring/123838/3 "2024-12-14T10:50:19Z")

</div>

Hello,  
Thank you so much for you reply.  
So, when I use `str[1:5]`, does it allocate new memory on the heap?

---

<div class="post-metadata">

**Author:** ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)\
**Post date:** [December 14, 2024, 11:04am UTC](https://discourse.julialang.org/t/a-question-about-substring/123838/4 "2024-12-14T11:04:03Z")

</div>

Yes, it creates a full copy of the string, stored on the heap

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [December 14, 2024, 12:05pm UTC](https://discourse.julialang.org/t/a-question-about-substring/123838/5 "2024-12-14T12:05:27Z")

</div>

Slight nitpick: the new `String` is not semantically required to be allocated on the heap. It’s extremely likely to end up there, but it’s not a semantic requirement.

That being said, the main difference between indexing and calling `SubString` stands - the former creates a copy, while the latter merely stores the indices into the existing `String`. `SubString` is what you get from `@view str[1:5]`.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [December 14, 2024, 10:36pm UTC](https://discourse.julialang.org/t/a-question-about-substring/123838/6 "2024-12-14T22:36:13Z")

</div>

It may or may not be obvious: the tradeoff of not creating a copy is that the parent string object cannot be freed by the GC. If that string is large and you don’t need to keep it around then you want to copy the part of the data you need.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 14, 2024, 10:40pm UTC](https://discourse.julialang.org/t/a-question-about-substring/123838/7 "2024-12-14T22:40:43Z")

</div>

once we have a moving GC, we could possibly do some tricks here where we could copy the substring and free the original if the original string dies (not sure if we could implement this in Julia, possibly with some sort of weaktef+finalizer?)

---

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [December 15, 2024, 7:21am UTC](https://discourse.julialang.org/t/a-question-about-substring/123838/8 "2024-12-15T07:21:47Z")

</div>

Hello,  
Thank you so much for your reply.  
What is GC?

---

<div class="post-metadata">

**Author:** ![vettert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vettert/32/30599_2.png) [@vettert](https://discourse.julialang.org/u/vettert)\
**Post date:** [December 15, 2024, 8:21am UTC](https://discourse.julialang.org/t/a-question-about-substring/123838/9 "2024-12-15T08:21:55Z")

</div>

Garbage collector; roughly speaking it removes objects that are not needed anymore from memory (“freeing” the memory). In Julia this is called automatically. In other languages, such as C, it’s the responsibility of the programmer.
