# Truncating padded text

**URL:** https://discourse.julialang.org/t/truncating-padded-text/110256
**Category:** General Usage
**Tags:** strings
**Created:** [February 5, 2024, 6:02pm UTC](https://discourse.julialang.org/t/truncating-padded-text/110256 "2024-02-05T18:02:09Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [February 5, 2024, 6:02pm UTC](https://discourse.julialang.org/t/truncating-padded-text/110256/1 "2024-02-05T18:02:09Z")

</div>

I’ve often wished that `rpad` and `lpad` would truncate to the given width if they’re already wider than `n` — do we have a function or succinct idiom for that somewhere?

---

<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: [February 5, 2024, 6:05pm UTC](https://discourse.julialang.org/t/truncating-padded-text/110256/2 "2024-02-05T18:05:30Z")

</div>

Not that I can recall. It could be implemented as a keyword option to `rpad` and `lpad`.

---

<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: [February 5, 2024, 6:05pm UTC](https://discourse.julialang.org/t/truncating-padded-text/110256/3 "2024-02-05T18:05:58Z")

</div>

One tricky part of this is that truncating text can make it longer (specifically with languages like Arabic where final characters sometimes have different rendering).

---

<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: [February 5, 2024, 6:08pm UTC](https://discourse.julialang.org/t/truncating-padded-text/110256/4 "2024-02-05T18:08:09Z")

</div>

> [@Oscar\_Smith](#):
>
> One tricky part of this is that truncating text can make it longer (specifically with languages like Arabic where final characters sometimes have different rendering).

Right now our `textwidth` function is monotonic, based on a relatively simple model for how terminals render characters. (Terminals don’t generally know about the kind of ligatures you are talking about, I think?)

Even for things as common as emoji characters, terminals don’t all agree on the widths, so it’s impossible to do perfect column alignment without having lower-level/terminal-specific knowledge of the text-rendering engine. See the infamous issue: [Julia doesn't like Pizza · Issue #3721 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/3721)

---

<div class="post-metadata">

### Author: ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)
#### Post date: [February 15, 2024, 2:29pm UTC](https://discourse.julialang.org/t/truncating-padded-text/110256/5 "2024-02-15T14:29:08Z")

</div>

If you use [Format.jl](https://github.com/JuliaString/Format.jl), you can do either `cfmt("%10.10s", str)` or `pyfmt("10.10s", str)`, to both pad and truncate to the given width & precision.  
Note that this is broken on [Formatting.jl](https://github.com/JuliaIO/Formatting.jl).  
Also note that `Printf.jl` currently has a bug with truncating strings that have characters that have textwidth \> 1 (I just discovered yesterday that while implementing the precision argument for strings for the Python style formatting)

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [February 15, 2024, 3:07pm UTC](https://discourse.julialang.org/t/truncating-padded-text/110256/6 "2024-02-15T15:07:59Z")

</div>

That’s not really working as I’d hope. Second thing I tried:

```julia
julia> using Format

julia> cfmt("%10.10s", "🍕🍕🍕🍕🍕🍕")
" 🍕🍕🍕🍕\xf0\x9f\x8d/LLVM"

```

With `--checkbounds=yes`:

```julia
julia> cfmt("%10.10s", "🍕🍕🍕🍕🍕🍕")
ERROR: BoundsError: attempt to access 23-element Vector{UInt8} at index [24]

```

---

<div class="post-metadata">

### Author: ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)
#### Post date: [February 15, 2024, 3:12pm UTC](https://discourse.julialang.org/t/truncating-padded-text/110256/7 "2024-02-15T15:12:36Z")

</div>

Ah - sorry! I haven’t gotten the fix for cfmt checked in yet. Later today!  
It _is_ already fixed for the Python format function (pyfmt).

julia\> pyfmt(“10.10s”, “🍕🍕🍕🍕🍕🍕”)  
“🍕🍕🍕🍕🍕”

---

<div class="post-metadata">

### Author: ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)
#### Post date: [February 15, 2024, 3:18pm UTC](https://discourse.julialang.org/t/truncating-padded-text/110256/8 "2024-02-15T15:18:58Z")

</div>

Also, here is an example of where `@sprintf/@printf` give the wrong result:

```julia
julia> @sprintf("%10.9s", "🍕🍕🍕🍕🍕🍕")
" 🍕🍕🍕🍕"

julia> textwidth(ans)
9

```

(it should be padded with 2 spaces, so that the result is 10 wide)

---

<div class="post-metadata">

### Author: ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)
#### Post date: [February 15, 2024, 7:39pm UTC](https://discourse.julialang.org/t/truncating-padded-text/110256/9 "2024-02-15T19:39:33Z")

</div>

It’s fixed now! I thought I’d gotten that part of the changes commited before I made the PR, but was just too quick for my own good!
