Golang Program to pad a string with 0's on the right side


In the Go programming language, strings are a built-in data type that represents sequences of characters. They are defined using double quotes (") and can contain any valid Unicode characters. In this article we will write a go language program to pad a string with 0 on the right side of it.

Method 1: Using User-defined function

In this method , We are going to creating an external function. The function accepts the string to be padded along with the length as argument and returns the final string after adding zeroes to right of it.

Algorithm

  • Step 1 − First, we need to import the fmt package.

  • Step 2 − Then, we need to create PadRight() function. This function accepts the string along with the length it should have as arguments.

  • Step 3 − The function then uses a while loop and adds a zero to the right of the string until the length of the string becomes equal to the length initialized.

  • Step 4 − Return the final result. Now start the main() function. Inside the main() initialize a string and store a value to it. further print the string on the screen.

  • Step 5 − Now, call the PadRight() function by passing the required arguments to it. Store the result obtained by the function in a new variable.

  • Step 6 − Print the string on the screen using fmt.Println() function.

Example

In this example we will write a go language program to pad a string with 0’s on the right side.

package main
import "fmt"

//creating a function to add zeroes to a string
func PadRight(str string, length int) string {
   for len(str) < length {
      str = str + "0"
   }
   return str
}

func main() {
   // initializing a string
   str := "123"
   newStr := PadRight(str, 6)
   fmt.Printf("The given string is: %s\n", str)
   fmt.Printf("Padded string is: %s\n", newStr)
}

Output

The given string is: 123
Padded string is: 123000

Method 2: Using Internal Function

In this method, we are going to use the internal function like Repeat(), Join(), bytes.Join() and buffer packages to get 0’s padded on the right side of string using Programming language.

Syntax

func Repeat(str string, count int) string

The Repeat() function is present in strings package and is used to create the copies of the string depending upon the integer variable specified. It accept two arguments one is the string that is to be repeated and other is the integer value named count. The function then returns the final string which contains the repetition of the provided string.

func Join(s []string, sep string) string

The join function is used to convert an array to string. This function is present in strings package. it takes two arguments first one is the array that we wish to convert and second is the separation with which the array elements should be separated once they are converted to strings and returns the final string.

Algorithm

  • Step 1 − First, we need to import the fmt and strings package.

  • Step 2 − Then, start the main() function. Inside the main() initialize a string variable and assign value to it. further print the string on the screen.

  • Step 3 − Now, call the strings.Repeat() function and pass the string along with the integer value to which it should be repeated as arguments to the function.

  • Step 4 − Pass the result obtained along with the string variable initialized above and get the array of strings containing all these values.

  • Step 5 − But we do not need array of strings to convert this array of string back to the strings pass this as argument to the function Join() and join it with an empty string.

  • Step 6 − Store the result in a variable and print it on the screen by using fmt.Println() function.

Example 1

In this example, we are going to use Repeat() and Join () to get the 0’s padded on the right-side of the string

package main
import (
   "fmt"
   "strings"
)

func main() {
   // initializing a string
   var str string = "123"
   var newStr string = strings.Join([]string{str, strings.Repeat("0", 6-len(str))}, "")
   fmt.Printf("The given string is: %s\n", str)
   fmt.Printf("Padded string is: %s\n", newStr)
}

Output

The given string is: 123
Padded string is: 123000

Example 2

In this example we will use the buffer package to pad a string with 0 on the right of a string.

package main
import (
   "bytes"
   "fmt"
   "strings"
)

func main() {
   var s string = "123"
   fmt.Println("The given string is:\n", s)
   var buffer bytes.Buffer
   buffer.WriteString(s)
   buffer.WriteString(strings.Repeat("0", 5-len(s)))
   var padded string = buffer.String()
   fmt.Println()
   fmt.Println("Padded string is:\n", padded)
}

Output

The given string is:
 123

Padded string is:
 12300

Example 3

In this example we will use the bytes.Join() function alone to pad a given string with zeroes to right side of it.

package main
import (
   "fmt"
   "strings"
)

func main() {
   var s string = "hello"
   fmt.Println("The given string is:\n", s)
   var padded string = s + strings.Repeat("0", 10-len(s))
   fmt.Println()
   fmt.Println("Padded string is:\n", padded)
}

Output

The given string is:
 hello

Padded string is:
 hello00000

Conclusion

We have successfully compiled and execute a Golang Program to pad a string with 0 to the right side along with examples. Here we have used both external and library functions in go to achieve the result.

Updated on: 17-Feb-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements