> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/ios/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/ios/swift/type/category/basic/string/str.pad.md).

# str.pad()

{% tabs %}
{% tab title="🌀 String + .pad()" %}
⬆️ 需要： [Character \* Int](/ios/swift/op/character-int.md)

````swift
// ────────────────────────────────────────────────────────────
// 2022.02.11 / .pad() change parameter order, 
//                     width -> _ width, align -> _ position
//                     remove `char` default argument
//                     PadAlign renamed PadPosition, case names renamed
// ────────────────────────────────────────────────────────────

extension String {
    
    /// ⭐️ position for string padding
    public enum PadPosition {
        case end, both, start
    }
    
    /// ⭐️ pad string with character.
    /// ```
    // "abc".pad("_", 10),       // _______abc
    // "abc".pad("_", 10, .end)  // abc_______
    // "abc".pad("_", 10, .both) // ___abc____
    /// ```
    public func pad(
        _     char: Character,
        _    width: Int,
        _ position: PadPosition = .start
    ) -> String 
    {
        // string longer than or equal to pad places
        if count >= width { return self }
        
        // string short than pad places
        let remainingPlaces = width - count
        let padStr   = char * remainingPlaces
        let leftPad  = remainingPlaces / 2
        let rightPad = remainingPlaces - leftPad
        
        switch position {
        case .end  : return self + padStr
        case .start: return padStr + self
        case .both : return (char * leftPad) + self + (char * rightPad) 
        }
    }
}
````

{% endtab %}

{% tab title="💈範例" %}
{% hint style="info" %}
👉 See: [Logger](/ios/swift/debugging/logger.md)
{% endhint %}
{% endtab %}

{% tab title="⬆️ 需要" %}

* [Character \* Int](/ios/swift/op/character-int.md)
  {% endtab %}

{% tab title="⬇️ 應用" %}

* [.binary(digits:)](/ios/swift/type/category/basic/numbers/binaryinteger/.binary-digits.md)
* [Logger](/ios/swift/debugging/logger.md)
  {% endtab %}

{% tab title="👥 相關" %}

* [String](/ios/swift/type/category/basic/string.md) extension.
  {% endtab %}
  {% endtabs %}

## History

{% tabs %}
{% tab title="✏️ archive" %}

1. 2022.02.11&#x20;
   {% endtab %}

{% tab title="1" %}

```swift
// ────────────────────────────────────────────────────────────
// 2022.01.28 * (v.1)          + operator `*`, .pad()
// 2022.01.30 r `String.Align` renamed to `String.PadAlign`
// ────────────────────────────────────────────────────────────

// ┌───────────────────────┐
// │    String + .pad()    │
// └───────────────────────┘

extension String {
    
    /// alignment for string padding
    public enum PadAlign {
        case left, center, right
    }
    
    /// pad string with character
    public func pad(
        width : Int,
        _ char: Character = " ",
        align : PadAlign  = .left
    ) -> String 
    {
        // string longer than or equal to pad places
        if count >= width { return self }
        
        // string short than pad places
        let remainingPlaces = width - count
        let padStr   = char * remainingPlaces
        let leftPad  = remainingPlaces / 2
        let rightPad = remainingPlaces - leftPad
        
        switch align {
        case .left  : return self + padStr
        case .right : return padStr + self
        case .center: return (char * leftPad) + self + (char * rightPad) 
        }
    }
}
```

{% endtab %}
{% endtabs %}
