
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Pad a String with Zeros on Left Side in Golang
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 are going to learn different techniques to pad a string with 0's on the left side.
Method 1: Using User-defined function
In this method, we will achieve this by 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 left of it.
Algorithm
Step 1 ? First, we need to import the fmt package.
Step 2 ? Then, we need to create PadLeft() 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 left 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 PadLeft() 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 left side.
package main import "fmt" //creating a function to add zeroes to a string func PadLeft(str string, length int) string { for len(str) < length { str = "0" + str } return str } func main() { // initializing a string str := "123" newStr := PadLeft(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: 000123
Method 2: Using in build Function
In this method, we are going to use internal function like repeat and join. Both the syntax are explained below ?
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.
func TrimLeft(str, cutset string) string
TrimLeft() function is present in strings package and is used to remove get a string after removing all the elements present in the left of that. It accepts two arguments. One is the string which is to be trimmed and other is the sub string that contains the elements that should be removed. The function then returns the final string after performing the respective updations.
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 article we will write a go language program to pad a string with 0 to the left by using the repeat() function.
package main import ( "fmt" "strings" ) func main() { // initializing a string var str string = "123" var newStr string = strings.Join([]string{strings.Repeat("0", 6-len(str)), 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: 000123
Example 2
In this example we will use the TrimLeft() function to pad a string with 0 on the left of a string.
package main import ( "fmt" "strings" ) func main() { // initializing a string var str string = "abc" var newStr string = strings.TrimLeft(fmt.Sprintf("%06s", str), " ") fmt.Printf("The given string is: %s\n", str) fmt.Printf("Padded string is: %s\n", newStr) }
Output
The given string is: abc Padded string is: 000abc
Example 3
In this example we will use the bytes.Join() and bytes.Repeat() function to pad a given string with zeroes.
package main import ( "bytes" "fmt" ) func main() { // initializing a string var str string = "abc" newStr := string(bytes.Join([][]byte{bytes.Repeat([]byte("0"), 6-len(str)), []byte(str)}, []byte(""))) fmt.Printf("The given string is: %s\n", str) fmt.Println("Padded string is: \n", newStr) }
Output
The given string is: abc Padded string is: 000abc
Conclusion
We have successfully compiled and execute a Golang Program to pad a string with 0 along with examples. Here we have used both external and library functions in go to achieve the result. Here we have used both external functions and library functions.