# Line feed by spaces

**URL:** https://discourse.julialang.org/t/line-feed-by-spaces/71439
**Category:** General Usage
**Tags:** strings, io
**Created:** [November 13, 2021, 9:59pm UTC](https://discourse.julialang.org/t/line-feed-by-spaces/71439 "2021-11-13T21:59:17Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![andrey2185](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrey2185/32/9889_2.png) [@andrey2185](https://discourse.julialang.org/u/andrey2185)
#### Post date: [November 13, 2021, 9:59pm UTC](https://discourse.julialang.org/t/line-feed-by-spaces/71439/1 "2021-11-13T21:59:17Z")

</div>

hello, can you tell me how to make a better function that performs line feeds by spaces, than what I did with my natural science education, i, j, l and += 1 ?

```julia
function feed_lines(str::String, l::Int)
    chars = collect(str)
    i = findfirst(' ', str)
    j = 1
    for (index, char) in pairs(chars)
        if char == ' '
            i = index
        end
        if j > l
            chars[i] = '\n'
            j = index - i
        end
        j += 1
    end
    return join(chars)
end

```

---

<div class="post-metadata">

### Author: ![andrey2185](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrey2185/32/9889_2.png) [@andrey2185](https://discourse.julialang.org/u/andrey2185)
#### Post date: [November 13, 2021, 10:01pm UTC](https://discourse.julialang.org/t/line-feed-by-spaces/71439/2 "2021-11-13T22:01:56Z")

</div>

although it seems not so bad

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [November 13, 2021, 11:09pm UTC](https://discourse.julialang.org/t/line-feed-by-spaces/71439/3 "2021-11-13T23:09:21Z")

</div>

> [@andrey2185](#):
>
> `function feed_lines(str::String, l::Int)`

Could you explain what is the role of integer `l`?

In my limited understanding the following seems to produce similar results:

```julia
feed_lines2(str::String) = join(split(str),"\n")

```

---

<div class="post-metadata">

### Author: ![andrey2185](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrey2185/32/9889_2.png) [@andrey2185](https://discourse.julialang.org/u/andrey2185)
#### Post date: [November 13, 2021, 11:56pm UTC](https://discourse.julialang.org/t/line-feed-by-spaces/71439/4 "2021-11-13T23:56:25Z")

</div>

this is line length, if line length \> l then replace last space (which \< l) to ‘\n’

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [November 14, 2021, 12:02am UTC](https://discourse.julialang.org/t/line-feed-by-spaces/71439/5 "2021-11-14T00:02:07Z")

</div>

Could you provide a minimum working example (MWE) to illustrate what it is supposed to do?

---

<div class="post-metadata">

### Author: ![andrey2185](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrey2185/32/9889_2.png) [@andrey2185](https://discourse.julialang.org/u/andrey2185)
#### Post date: [November 14, 2021, 12:28am UTC](https://discourse.julialang.org/t/line-feed-by-spaces/71439/6 "2021-11-14T00:28:21Z")

</div>

sure

```julia
julia> function feed_lines(str::String, l::Int)
           chars = collect(str)
           i = findfirst(' ', str)
           j = 1
           for (index, char) in pairs(chars)
               if char == ' '
                   i = index
               end
               if j > l
                   chars[i] = '\n'
                   j = index - i
               end
               j += 1
           end
           return join(chars)
       end
feed_lines (generic function with 1 method)

julia> feed_lines("""hello, can you tell me how to make a better function that performs line feeds by spaces, than what I did with my natural science education, i, j, l and += 1 ?""", 30) |> println

hello, can you tell me how to
make a better function that
performs line feeds by spaces,
than what I did with my
natural science education, i,
j, l and += 1 ?

```

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [November 14, 2021, 7:38am UTC](https://discourse.julialang.org/t/line-feed-by-spaces/71439/7 "2021-11-14T07:38:45Z")

</div>

Just an idea (not quite doing what you ask for):

```julia
julia> join(join.(Iterators.partition(s, 30) |> collect, ""), "\n") |> println
hello, can you tell me how to
make a better function that pe
rforms line feeds by spaces, t
han what I did with my natural
 science education, i, j, l an
d += 1 ?

```

where `s` is your example string.

(Do we have an efficient “word iterator” somewhere?)

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [November 14, 2021, 7:57am UTC](https://discourse.julialang.org/t/line-feed-by-spaces/71439/8 "2021-11-14T07:57:56Z")

</div>

This is rather compact:

```julia
julia> function feed_lines2(str, l)
           line_breaks = findnext.(' ', str, l:l:length(str))
           join([i in line_breaks ? '\n' : c for (i,c) in pairs(str)])
       end
feed_lines2 (generic function with 1 method)

julia> feed_lines2("""hello, can you tell me how to make a better function that performs line feeds by spaces, than what I did with my natural science education, i, j, l and += 1 ?""", 30) |> println
hello, can you tell me how to
make a better function that performs
line feeds by spaces, than
what I did with my natural
science education, i, j, l and
+= 1 ?

```

However, I think it will be (a bit) less efficient than your variant.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [November 14, 2021, 8:06am UTC](https://discourse.julialang.org/t/line-feed-by-spaces/71439/9 "2021-11-14T08:06:14Z")

</div>

A version that uses `SubString` and also returns the number of rows printed:

```julia
function feed_lines3(str, n)
    ix = [1; findall(' ', str); length(str)]
    j1 = zero(ix)
    j2 = zero(ix)
    nr = 1
    j1[1] = 1
    for i in 2:length(ix)
        if ix[i] - j1[nr] > n
            j2[nr] = ix[i-1]
            nr += 1
            j1[nr] = ix[i-1] + 1
        elseif i == length(ix)
            j2[nr] = ix[end]
        end
    end
    join(SubString.(str, view(j1,1:nr), view(j2,1:nr)), "\n"), nr
end

println(feed_lines3(str, 30)[1])
# result:
hello, can you tell me how to
make a better function that
performs line feeds by spaces,
than what I did with my
natural science education, i,
j, l and += 1 ?

```

This version should be further optimized, as only one `j` array seems necessary.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [November 14, 2021, 12:22pm UTC](https://discourse.julialang.org/t/line-feed-by-spaces/71439/10 "2021-11-14T12:22:09Z")

</div>

FWIW, a simpler alternative and a bit faster:

```julia
function feed_lines4(str, n)
    ix = [1; findall(' ', str); length(str)]
    nx = length(ix)
    j = 1;
    s2 = ""
    for i in 2:nx
        if ix[i] - j > n
            s2 = s2 * view(str,j:ix[i-1]) * "\n"
            j = ix[i-1] + 1
        elseif i == nx
            s2 = s2 * view(str,j:ix[nx]) * "\n"
        end
    end
    return s2
end

println(feed_lines4(str,30))
# result:
hello, can you tell me how to
make a better function that
performs line feeds by spaces,
than what I did with my
natural science education, i,
j, l and += 1 ?

```

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [November 14, 2021, 1:48pm UTC](https://discourse.julialang.org/t/line-feed-by-spaces/71439/11 "2021-11-14T13:48:33Z")

</div>

There is also [https://github.com/carlobaldassi/TextWrap.jl](https://github.com/carlobaldassi/TextWrap.jl)

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [November 14, 2021, 3:44pm UTC](https://discourse.julialang.org/t/line-feed-by-spaces/71439/12 "2021-11-14T15:44:58Z")

</div>

@stevengj, the `TextWrap.jl` package although more general seems for the OP example to be slower than the above solutions _(maybe_ `~3x` _when comparing using_ @time _run multiple times)_.
