# String padding method is broken?

**URL:** <https://forums.swift.org/t/string-padding-method-is-broken/14417>\
**Category:** Using Swift\
**Tags:** string\
**Created:** [July 12, 2018, 3:45pm UTC](https://forums.swift.org/t/string-padding-method-is-broken/14417 "2018-07-12T15:45:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![robnik](https://avatars.discourse-cdn.com/v4/letter/r/46a35a/32.png) [@robnik](https://forums.swift.org/u/robnik)\
**Post date:** [July 12, 2018, 3:45pm UTC](https://forums.swift.org/t/string-padding-method-is-broken/14417/1 "2018-07-12T15:45:29Z")

</div>

It looks like `String`'s `padding` method is broken. It's docs say it will pad to _characters_, not unicode scalars.

Here's a simple example. This string uses a combining character.

```
var str = "аа́а"
str.count ==> 3
str.unicodeScalars.count ==> 4
str.padding(toLength: 3, withPad: " ", startingAt: 0) ==> "aá"

```

Rob

---

<div class="post-metadata">

**Author:** ![lancep](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lancep/32/1509_2.png) [@lancep](https://forums.swift.org/u/lancep)\
**Post date:** [July 12, 2018, 4:35pm UTC](https://forums.swift.org/t/string-padding-method-is-broken/14417/2 "2018-07-12T16:35:10Z")

</div>

The `padding` method is actually a method provided by `NSString`, so you'll get this behavior in Objective C as well.

It also depends on how your string is formatted:

```swift
func printStuff(str: String) {
  let nsstr = NSString(string: str)
  
  print(str.count, nsstr.length, str.unicodeScalars.count)
  
  print(str.padding(toLength: 3, withPad: " ", startingAt: 0))
}

let s1 = "a\u{e1}a"
let s2 = "aa\u{301}a"

printStuff(str: s1)
printStuff(str: s2)

```

Prints:

> 3 3 3  
> aáa  
> 3 4 4  
> aá

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [July 12, 2018, 5:02pm UTC](https://forums.swift.org/t/string-padding-method-is-broken/14417/3 "2018-07-12T17:02:46Z")

</div>

Can you file a documentation bug with Apple ([https://bugreport.apple.com](https://bugreport.apple.com)) to refer to Unicode scalars rather than characters?

---

<div class="post-metadata">

**Author:** ![robnik](https://avatars.discourse-cdn.com/v4/letter/r/46a35a/32.png) [@robnik](https://forums.swift.org/u/robnik)\
**Post date:** [July 12, 2018, 5:14pm UTC](https://forums.swift.org/t/string-padding-method-is-broken/14417/4 "2018-07-12T17:14:47Z")

</div>

Sure, I can do that. Working by character rather than code point seems more useful and more consistent with Swift strings, but I guess if it's an old Objective-C method, the behavior can't be changed now. Fortunately this is simple to implement.

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [July 12, 2018, 5:15pm UTC](https://forums.swift.org/t/string-padding-method-is-broken/14417/5 "2018-07-12T17:15:44Z")

</div>

Oof, now I wonder if it really is Unicode scalars or if it's UTF-16 code units.

---

<div class="post-metadata">

**Author:** ![robnik](https://avatars.discourse-cdn.com/v4/letter/r/46a35a/32.png) [@robnik](https://forums.swift.org/u/robnik)\
**Post date:** [July 12, 2018, 5:27pm UTC](https://forums.swift.org/t/string-padding-method-is-broken/14417/6 "2018-07-12T17:27:33Z")

</div>

I think it's UTF-16. I tried some emoji strings that have different numbers for `unicodeScalars.count` and `utf16.count` and then `padding(...)` destroys the emojis.

---

<div class="post-metadata">

**Author:** ![Martin](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@Martin](https://forums.swift.org/u/Martin)\
**Post date:** [July 12, 2018, 5:28pm UTC](https://forums.swift.org/t/string-padding-method-is-broken/14417/7 "2018-07-12T17:28:36Z")

</div>

It is UTF-16 code units (i.e. the `unichar` s of `NSString`):

```
let s = "🏁"
print(Array(s.unicodeScalars)) // ["\u{0001F3C1}"]
print(Array(s.utf16)) // [55356, 57281]

let p = s.padding(toLength: 3, withPad: " ", startingAt: 0)
print(Array(p.unicodeScalars)) // ["\u{0001F3C1}", " "]
print(Array(p.utf16)) // [55356, 57281, 32]

```

---

<div class="post-metadata">

**Author:** ![hexdreamer](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hexdreamer/32/430_2.png) [@hexdreamer](https://forums.swift.org/u/hexdreamer)\
**Post date:** [July 13, 2018, 6:42pm UTC](https://forums.swift.org/t/string-padding-method-is-broken/14417/8 "2018-07-13T18:42:25Z")

</div>

Good analysis! This looks like a good argument for treating Strings as extended grapheme clusters (most of the time).

---

<div class="post-metadata">

**Author:** ![berik](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/berik/32/12477_2.png) [@berik](https://forums.swift.org/u/berik)\
**Post date:** [August 1, 2020, 1:12pm UTC](https://forums.swift.org/t/string-padding-method-is-broken/14417/9 "2020-08-01T13:12:10Z")

</div>

```
str.padding(toLength: 3, withPad: " ", startingAt: 0)

```

Is still broken for unicode strings. Is there anything I can help with to improve this?
