Skip to main content
added 13 characters in body
Source Link
possen
  • 9.3k
  • 2
  • 41
  • 51

The other answers are good if you are dealing only with numbers using the format string, but this is good when you may have strings that need to be padded (although admittedly a little diffent than the question asked, seems similar in spirit). Also, be careful if the string is longer than the pad.

 let str = "a str"  let padAmount = max(10, str.count)  String(repeatElement("-", count: padAmount - str.count)) + str 

Output "-----a str"

The other answers are good if you are dealing only with numbers using the format string, but this is good when you may have strings that need to be padded (although admittedly a little diffent than the question asked, seems similar in spirit). Also, be careful if the string is longer than the pad.

let str = "a str" let padAmount = 10 String(repeatElement("-", count: padAmount - str.count)) + str 

Output "-----a str"

The other answers are good if you are dealing only with numbers using the format string, but this is good when you may have strings that need to be padded (although admittedly a little diffent than the question asked, seems similar in spirit). Also, be careful if the string is longer than the pad.

 let str = "a str"  let padAmount = max(10, str.count)  String(repeatElement("-", count: padAmount - str.count)) + str 

Output "-----a str"

added 13 characters in body
Source Link
possen
  • 9.3k
  • 2
  • 41
  • 51

The other answers are good if you are dealing only with numbers using the format string, but this is good when you may have strings that need to be padded (although admittedly a little diffent than the question asked, seems similar in spirit). Also, be careful if the string is longer than the pad.

let str = "a str" let padAmount = 10 String(repeatElement("-", count: padAmount - str.count)) + str 

Output "-----a str"

The other answers are good if you are dealing only with numbers using the format string, but this is good when you may have strings that need to be padded.

let str = "a str" let padAmount = 10 String(repeatElement("-", count: padAmount - str.count)) + str 

Output "-----a str"

The other answers are good if you are dealing only with numbers using the format string, but this is good when you may have strings that need to be padded (although admittedly a little diffent than the question asked, seems similar in spirit). Also, be careful if the string is longer than the pad.

let str = "a str" let padAmount = 10 String(repeatElement("-", count: padAmount - str.count)) + str 

Output "-----a str"

added 13 characters in body
Source Link
possen
  • 9.3k
  • 2
  • 41
  • 51

The other answers are good if you are dealing only with numbers using the format string, but this is good when you may have numbers or strings that need to be padded.

let str = "a str" let padAmount = 10 String(repeatElement("-", count: padAmount - str.count)) + str 

Output "-----a str"

The other answers are good if you are dealing only with numbers using the format string, but this is good when you may have numbers or strings

let str = "a str" let padAmount = 10 String(repeatElement("-", count: padAmount - str.count)) + str 

Output "-----a str"

The other answers are good if you are dealing only with numbers using the format string, but this is good when you may have strings that need to be padded.

let str = "a str" let padAmount = 10 String(repeatElement("-", count: padAmount - str.count)) + str 

Output "-----a str"

Source Link
possen
  • 9.3k
  • 2
  • 41
  • 51
Loading