# Return of split

**URL:** https://discourse.julialang.org/t/return-of-split/122987
**Category:** New to Julia
**Tags:** function
**Created:** [November 23, 2024, 10:17am UTC](https://discourse.julialang.org/t/return-of-split/122987 "2024-11-23T10:17:22Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![martin\_sanchez](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martin_sanchez/32/207525_2.png) [@martin\_sanchez](https://discourse.julialang.org/u/martin_sanchez)
#### Post date: [November 23, 2024, 10:17am UTC](https://discourse.julialang.org/t/return-of-split/122987/1 "2024-11-23T10:17:22Z")

</div>

I was using split(str,separator) and it returned SubString (I verified it with typeof) and wa using the result in later functions:

```julia
function instruct_processor(db::Dict,op::String)
    arr=split(op," ",limit=2)
    print("primer arr: ")
    println(typeof(arr))

```

and suddenly it returns a vector of substrings:

```julia
Vector{SubString{String}}

```

But what really intrigues me is that even though I was receiving an error

Because of this function:

```julia
function _long_adecuada(len::Integer,arr::Vector{SubString{String}})
    length(arr)!=len && throw(NumberArgsError())
   
end

```

```julia
ERROR: LoadError: MethodError: no method matching _long_adecuada(::Int64, ::Vector{SubString{String}})

Closest candidates are:
  _long_adecuada(::Integer, ::SubString{String})

```

The other functions that also received Substring did not show error:

This runs:

````julia
type or paste code here
```function insertField!(db::Dict,params::SubString{String})
    typeof(params)
    arr=split(params," ",limit=3)
    println(typeof(arr))
    println(length(arr))
````

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [November 23, 2024, 10:26am UTC](https://discourse.julialang.org/t/return-of-split/122987/2 "2024-11-23T10:26:06Z")

</div>

> [@martin\_sanchez](#):
>
> I was using split(str,separator) and it returned SubString

For example? (Give minimal reproducer.)

---

<div class="post-metadata">

### Author: ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)
#### Post date: [November 23, 2024, 10:30am UTC](https://discourse.julialang.org/t/return-of-split/122987/3 "2024-11-23T10:30:41Z")

</div>

Please see the post [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757). It will make it easier to help with your questions.

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [November 23, 2024, 11:02am UTC](https://discourse.julialang.org/t/return-of-split/122987/4 "2024-11-23T11:02:17Z")

</div>

It’s hard to follow what you did and what works or not for you but the docstring for `split` says

> Split str into an array of substrings

so focus on showing how to reproduce

> I was using split(str,separator) and it returned SubString

and we can tell you whether there is some misunderstanding or you have found a bug.

---

<div class="post-metadata">

### Author: ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)
#### Post date: [November 23, 2024, 11:07am UTC](https://discourse.julialang.org/t/return-of-split/122987/5 "2024-11-23T11:07:44Z")

</div>

A substring is like a view into a string, i.e., references into an existing string instead of allocating a new data structure. Many functions for splitting, matching etc on strings return substrings. Here is an example:

```julia-repl
julia> txt = "Hi there"
"Hi there"

julia> sub = match(r"Hi", txt).match
"Hi"

julia> typeof(sub)
SubString{String}

julia> parent(sub) # The underlying original string
"Hi there"

julia> split(txt, " ")
2-element Vector{SubString{String}}:
 "Hi"
 "there"

julia> parent.(split(txt, " "))
2-element Vector{String}:
 "Hi there"
 "Hi there"

```

Note that a `SubString` is not a `String`, i.e., a function with signature `op::String` will not work as expected:

```julia-repl
julia> fun(op::String) = length(op)
fun (generic function with 1 method)

julia> fun(txt)
8

julia> fun(sub)
ERROR: MethodError: no method matching fun(::SubString{String})

```

If in doubt, leave out the type signature all together (would suggest that for beginners anyways) or use `AbstractString` instead which will match all of its subtypes:

```julia-repl
julia> subtypes(AbstractString)
6-element Vector{Any}:
 Base.AnnotatedString
 Core.Compiler.LazyString
 LazyString
 String
 SubString
 SubstitutionString

```

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [November 23, 2024, 11:30am UTC](https://discourse.julialang.org/t/return-of-split/122987/7 "2024-11-23T11:30:42Z")

</div>

I don’t see anything surprising there.

> `arr=split(op," ",limit=2)`

gives you `arr` as a `Vector{SubString{String}}`.

Then `arr[2]` extracts one element from the vector which gives you a `SubString{String}`. When you send that single element to `insertField!` it complains because it requires a whole vector, not a single element.

---

<div class="post-metadata">

### Author: ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)
#### Post date: [November 23, 2024, 11:34am UTC](https://discourse.julialang.org/t/return-of-split/122987/8 "2024-11-23T11:34:21Z")

</div>

The error says that `insertField!` expected a vector in its second argument (as per your type signature `Vector{SubString{String}}`), but was called with a `SubString{String}`, i.e., as single element of such a vector. Have not checked if this function actually needs to get passed a vector – but as `split` is called on `params` it most likely does not.

In any case, such a specific type signature usually restricts code unnecessarily. Consider `AbstractVector{<:AbstractString}` instead.
