Swift Program to pad a string with 0's on left side


In Swift, pad a string with 0’s is to add 0 in the given string either on the left side or on the right side, for example,34000 or 0003232. Here we are going to pad a string with 0’s on the left side.

Example

Input: String = "151"
newLength = 6 Output: 000151

Here, we pad the original string with three zeros on the left side.

Algorithm

  • Step 1 − Create a function which takes the original string and the length of the new string as arguments.

  • Step 2 − Calculates the total number of zeros that need to be added in the given string to make the length equal to the new length by subtracting the original length from the new length.

  • Step 3 − Now use the String(repeating:count:) method to create a new string consisting of zeros followed by the original string.

  • Step 4 − Return the resultant string.

  • Step 5 − Create a string.

  • Step 6 − Call the function and pass a string and the size of the new string.

  • Step 7 − Display the result.

Example 1

In the following Swift program, we will pad a string with 0’s on the left side. So, create a function which takes a string and the total length of the padded string as arguments. Now find the difference between the desired length and the original lengths, and then create a new string consisting of that many zeros using the String() initializer. After that concatenate the original string with the zero-padded string and return the final string.

import Foundation
import Glibc

func padZeroOnLeft(s: String, length: Int) -> String {
   let padZeroSize = max(0, length - s.count)
   let newStr = String(repeating: "0", count: padZeroSize) + s
   return newStr
}

let originalStr = "8297"
let newString = padZeroOnLeft(s: originalStr, length: 9)
print("Original String is \(originalStr)")
print("Padded String is \(newString)")

Output

Original String is 8297
Padded String is 000008297

Example 2

In the following Swift program, we will pad a string with 0’s on the left side. So, create a function which takes a string and the total length of the padded string as arguments. This function will create a copy of the original string. Then it uses while loop till the desired length of the padded string. Inside the loop it concatenates 0 with the resultant string on the left side and when the length of the desired string is equal to the given string then the loop will terminated and return the resultant string.

import Foundation
import Glibc

func padString(input: String, strlength: Int) -> String {
   var paddedStr = input
    
   while paddedStr.count < strlength {
      paddedStr = "0" + paddedStr
   }
    
   return paddedStr
}

// Example usage
let myString = "843904"
let resStr = padString(input:myString, strlength: 19)
print("Original String: ", myString)
print("Padded string with zeros on left side:", resStr)

Output

Original String:  843904
Padded string with zeros on left side: 0000000000000843904

Conclusion

So this is how we can pad a string with 0’s on the left side. Using the methods you can pad as many as zeros or any other character you want on the left side of the string. You can use these methods when you want to add a specified character multiple times at the start of the given string.

Updated on: 14-Jun-2023

327 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements