Instance Method

padding(to​Length:​with​Pad:​starting​At:​)

Returns a new string formed from the receiver by either removing characters from the end, or by appending as many occurrences as necessary of a given pad string.

Declaration

func padding(toLength newLength: Int, withPad padString: String, startingAt padIndex: Int) -> String

Parameters

new​Length

The new length for the receiver.

pad​String

The string with which to extend the receiver.

pad​Index

The index in pad​String from which to start padding.

Return Value

A new string formed from the receiver by either removing characters from the end, or by appending as many occurrences of pad​String as necessary.

Discussion

Here are some examples of usage:

[@"abc" stringByPaddingToLength: 9 withString: @"." startingAtIndex:0];
    // Results in "abc......"
 
[@"abc" stringByPaddingToLength: 2 withString: @"." startingAtIndex:0];
    // Results in "ab"
 
[@"abc" stringByPaddingToLength: 9 withString: @". " startingAtIndex:1];
    // Results in "abc . . ."
    // Notice that the first character in the padding is " "