# String from iterator (HasEltype(), which is UInt8, HasLength())

**URL:** <https://discourse.julialang.org/t/string-from-iterator-haseltype-which-is-uint8-haslength/12430>\
**Category:** Internals & Design\
**Tags:** question\
**Created:** [July 17, 2018, 11:05am UTC](https://discourse.julialang.org/t/string-from-iterator-haseltype-which-is-uint8-haslength/12430 "2018-07-17T11:05:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 17, 2018, 11:05am UTC](https://discourse.julialang.org/t/string-from-iterator-haseltype-which-is-uint8-haslength/12430/1 "2018-07-17T11:05:23Z")

</div>

This question refers to v0.7.

I have an _ex ante_ known number n of integers 0 \le \ldots \le 127, which are computed from something (I can make an iterator). They represent a string (of ASCII characters).

The number of characters is small (1 to 8). I am looking for a fast way to construct a `String`.

I can build up a `Vector{UInt8}`, but I am unsure if `String(::Vector{UInt8})` copies, or if I need to do something special to make it not copy, eg somehow guarantee that the vector does not share structure.

Example:

````julia
# imagine I have an iterator with HasLength() and HasEltype(), latter is UInt8
iter = (i for i in UInt8.(97:99))

# does this copy the result of collect? it is unclear to me.
String(collect(iter))```
````

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [July 17, 2018, 11:16am UTC](https://discourse.julialang.org/t/string-from-iterator-haseltype-which-is-uint8-haslength/12430/2 "2018-07-17T11:16:03Z")

</div>

```julia
  String(v::AbstractVector{UInt8})

  Create a new String object from a byte vector v containing UTF-8 encoded characters. If v is Vector{UInt8} it will be truncated to zero length and future modification of v cannot affect the
  contents of the resulting string. To avoid truncation use String(copy(v)).

```

```julia
julia> b = collect(iter)
3-element Array{UInt8,1}:
 0x61
 0x62
 0x63

julia> String(b)
"abc"

julia> b
0-element Array{UInt8,1}

```

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [July 17, 2018, 7:33pm UTC](https://discourse.julialang.org/t/string-from-iterator-haseltype-which-is-uint8-haslength/12430/3 "2018-07-17T19:33:46Z")

</div>

Though standard `Vector{UInt8}` inputs are copied. They are resized to only enforce a consistent behavior. You can use `Base.StringVector` to create a special `Vector{UInt8}` whose contents will be reused without copying by `String`.
