Golang program to remove leading zeros


Removing any zeros that appear at the start of a number or string is referred to as "removing leading zeros" in Golang. Leading zeroes are frequently included for formatting or alignment needs, although they don't convey any significant information. They can be eliminated to improve the number's or string's usability and human-readableness. It's important to keep in mind that leading zeroes can occasionally be crucial for specific applications, such as some financial or scientific ones. Let’s see through this program to get a clear understanding of the concept. In this program we will learn how to remove leading zeros using different set of examples.

Method 1: Using TrimLeft Function

This method uses the TrimLeft function to remove all leading zeros ("0") from the input string "0000892", resulting in the output string "892". Let’s see its execution via Example: and algorithm.

Syntax

TrimLeft()

To remove preceding spaces or particular characters from a string, use the TrimLeft() function. It requires two arguments: the string you wish to change and the character or characters you wish to strike out from the string's beginning. The altered string is given back.

Alogrithm

  • Step 1 − Create a package main and declare fmt(format package) and strings package in the program where main produces executable Example:s and fmt helps in formatting input and output.

  • Step 2 − Create a main function and inside that function make a string initial_value variable and give it a value with leading zeros.

  • Step 3 − Using the TrimLeft function on the initial_value variable, delete the character (in this case, "0"), sending it as an argument.

  • Step 4 − Create a new variable output and assign the TrimLeft function's output to it.

  • Step 5 − Print the output on the console using fmt.Println() function where ln means new line.

  • Step 6 − The way the TrimLeft function operates is by eliminating all the characters you specify from a string's start. It eliminates all leading zeros in this instance. The output variable is then set to the result, which is subsequently printed to the console.

Example

In this example, we will see how to remove leading zeros using TrimLeft() function

package main
import (
   "fmt" 
   "strings"
)

func main() {
   initial_value := "0000000000892" //create a string with leading zeros
   fmt.Println("The initial input value is:", initial_value)

   output := strings.TrimLeft(initial_value, "0")//remove leading zeros using built-in function
   fmt.Println("The result after removing leading zeros is:")
   fmt.Println(output) //print output on the console
}

Output

The initial input value is: 0000000000892
The result after removing leading zeros is:
892

Method 2: Using loop to iterate through characters of string

In this method, each character in the input string is iterated through using a loop in this Example:. If the character is a zero and still in the leading zeros during the loop, it is not included to the result. Otherwise, it is checked. All further characters are added to the result after the first non-zero character is detected, setting the leading_zero variable to false. There won't be any leading zeros in the result string. Let’s see through Example: and algorithm.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable Example:s and fmt helps in formatting input and output.

  • Step 2 − Make a method called removeLeadingZeros that accepts a string named str as an input.

  • Step 3 − Declare the variables leading_zero as true and output as an empty string.

  • Step 4 − Use a for loop to iterate through each character in the input string.

  • Step 5 − Check if leading_zero is false or the current character is not a zero within the loop. Add the current character to the result string and change leading_zero to false if one of these conditions is true.

  • Step 6 − After the loop terminates, provide the output string back.

  • Step 7 − Call the removeLeadingZeros function from the main function and supply the initial_value string. Put the outcome in the output variable.

  • Step 8 − The output variable will be printed on the console using fmt.Println() function where ln means new line.

Example

In this example, we will learn how to remove leading zeros using loop to iterate through characters of string

package main
import (
   "fmt"
)

func removeLeadingZeros(str string) string {
   var output string    //create an empty string
   leading_zero := true     //set the flag as true 
   for _, ch := range str {
      if ch != '0' || !leading_zero {
         output += string(ch)
         leading_zero = false
      }
   }
   return output //return the output with no leading zeros
}

func main() {
   initial_value := "000000000892"   //create a string with leading zeros
   fmt.Println("The initial input value is:", initial_value)
   output := removeLeadingZeros(initial_value)
   fmt.Println("The result after removing leading zeros is:")
   fmt.Println(output)  //print the output on the console with no leading zeros
}

Output

The initial input value is: 000000000892
The result after removing leading zeros is:
892

Conclusion

We executed the program of removing leading zeros using two examples. In the first example we used the built-in function TrimLeft and in the second example we used a for loop to iterate through each character. Both the programs give similar output.

Updated on: 20-Feb-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements