Swift Program to Remove leading zeros


While working with strings sometimes we encounter some numeric strings whose leading numbers are zeros, e.g. 000003232, 00321, etc. To remove leading zeros from a numeric string −

  • Using removeFirst() and hasPrefix() methods

  • Using firstIndex() and dropFirst() methods

Example

Input: String = "000003231"
                 
Output: "3231 "

Here, the input string contains 5 leading zeros so we remove these zeros from the input string so the resultant string is 3231.

Method 1: Using removeFirst() and hasPrefix() methods

So to remove leading zeros from numeric strings Swift provides inbuilt methods named as removeFirst() and hasPrefix(). The hasPrefix() method is used to check if the given string starts with “0”. If the string starts with zero, then we use the removeFirst() function to remove all the prefix zeros from the string.

Syntax

func hasPrefix(s:String)

func removeFirst()

Here hasPrefix() function takes a single parameter which is the string we want to check if it is present in the prefix or not. Whereas the removeFirst () function does not take any parameter and removes the first character from the string.

Algorithm

  • Step 1 − Create a function that takes an input string as an argument.

  • Step 2 − Inside the function create a copy of the original string.

  • Step 3 − Then run a while loop and check if the string starts with 0 and the length of the string is greater than 1.

  • Step 4 − If both the conditions are true, then remove the first character from the string using the removeFirst() function. This process continues till the loop encounters non-zero elements.

  • Step 5 − Return the resultant string.

  • Step 6 − Create a string.

  • Step 7 − Call the function and pass the string in it.

  • Step 8 − Display output.

Example

In the following Swift program, we will remove leading zeros from the given string. So for that, we create a function which takes the input string and removes leading zeros. It first creates a copy of the original string and then runs a while loop to check if the string starts with 0 using the hasPrefix() function and if the length of the string is greater than 1. If both the conditions are true, then it uses the removeFirst() function to remove the first 0 from the string. This process continues till all the leading zeros are removed from the string and after that, this function returns a resultant string.

import Foundation
import Glibc

func deleteLeadingZeros(inputStr: String) -> String {
  var resultStr = inputStr
    
  while resultStr.hasPrefix("0") && resultStr.count > 1 {
   resultStr.removeFirst()
  }
  return resultStr
}

let enteredString = "0000037763986"
let output = deleteLeadingZeros(inputStr: enteredString)
print("Original String:", enteredString)
print("Modified String(After deleting leading zeros):", output)

Output

Original String: 0000037763986
Modified String(After deleting leading zeros): 37763986

Method 2: Using firstIndex() and dropFirst() methods

To remove leading zeros from numeric strings we use firstIndex() and dropFirst() methods. The firstIndex() method is used to get the first index of the specified value that satisfy the given criteria from the given sequence. If the element doesnot found, then it will return nil. The dropFirst() function is used to get all the elements after the specified number of elements.

Syntax

func firstIndex(where: mclosure)

func dropFirst(x)

Here hasPrefix() function takes a single parameter which is the closure that takes an item as an argument and return a boolean value which represent whether the given item pass the criteria or not. Whereas dropFirst() method take one parameter which represent the number of elements to drop from the beginning of the given sequence.

Algorithm

  • Step 1 − Create a function that takes an input string as an argument.

  • Step 2 − Inside the function find the index of first non-zero element from the given string.

  • Step 3 − If a non-zero element is then create a substring form the index till teh end of teh given substring. And remove the first character from the substring using dropFirts() method.

  • Step 4 − Return the resultant string.

  • Step 5 − Create a string.

  • Step 6 − Call the function and pass the string in it.

  • Step 7 − Display output.

Example

In the following Swift program, we will remove leading zeros from the given string. So for that, we create a function which takes the input string and removes leading zeros. It uses firstIndex(where:) method to find the index of the first character who is not equal to 0. If no non-zero character is found then that means the string only contains zero and it will return empty string. If a non-zero character is found then it creates a substring from that index to the end of the given string and remove the first character from the substring using dropFirst() method and finally return a string after removing leading zeros.

import Foundation
import Glibc

func deleteLeadingZeros(str: String) -> String {
   guard let NonZeroIndex = str.firstIndex(where: { $0 != "0" }) else {
      return ""
   }
   return String(str[NonZeroIndex...].dropFirst())
}

let inputStr = "0000093872"
let resultStr = deleteLeadingZeros(str: inputStr)

print("Original String:", inputStr)
print("String after removing leading zeros:", resultStr) 

Output

Original String: 0000093872
String after removing leading zeros: 3872

Conclusion

So this is how we can remove leading zeros from the given string. The discussed methods can remove any number of leading zeros from the input string. These methods removes only leading zeros if you want to remove zeros from both sides of the given string, then you can use the trimmingCharacters() function.

Updated on: 14-Jun-2023

614 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements