Golang Program to trim a string from the left 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 trim a string from the left side.

Method 1: Using User-Defined function

In this method, we are going to create a user-defined function with for loop to iterate over the string. It will compare the character of the string to the character from the left side to be removed. If they are equal then remove that character from the string and print the remaining string.

Algorithm

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

  • Step 2 − Then start the main() function. Inside the main() initialize a string and assign elements to it. further print the string on the screen by using fmt.Println() function.

  • Step 3 − Now, initialize a variable of integer data type and use for loop to iterate over the string. Then compare each character of the string to the left characters that are to be stored in the string.

  • Step 4 − If the character is found then store its index and break the for loop.

  • Step 5 − Now initialize a new string to store the result and store in it the elements of characters from the left index character’s index.

  • Step 6 − Now print this newly formed string on the screen by using fmt.Println() function.

Example

In this example we will write a go language program to trim a string from the left by using a for loop.

package main
import (
   "fmt"
)

func main() {
   var str string = "a this is a string b"
   fmt.Println("The given string is:\n", str)
   fmt.Println()
   var left int
   for i, c := range str {
      if c != 'a' {
         left = i
         break
      }
   }
   trimmed := str[left:]
   fmt.Println("The string obtained after trimming the above string from both sides is:\n", trimmed)
}

Output

The given string is:
 a this is a string b

The string obtained after trimming the above string from both sides is:
  this is a string b

Method 2: Using Pre-defined function

In this method, we are going to use internal Trim function of Golang to trim the left of a string – TrimLeft() and TrimPrefix(). These both function are explained below −

Syntax

func TrimLeft(str string, cutstr string) string

The TrimLeft() function is present in strings package and is used to remove the elements from left side of it. The function accepts two arguments one of them is the string from which the elements are to be removed and second is the substring which is to be removed. The function then returns the final string after removing the occurrences of the given substring from the left.

func TrimPrefix(str, prefix string) string

TrimPrefix() function is an inbuilt function in go language present in strings package and is used to remove a particular prefix from the string. The function accepts two variables as arguments one is the string from which the elements are to be removed and other is the prefix substring that is to be removed. After removing the substring the function returns the string as the output.

Algorithm

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

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

  • Step 3 − Now, Initialize a variable of string data type and call the TrimLeft() function. Pass the string as well as the substring to be removed as arguments to the function and store the final result in the newly initialized variable.

  • Step 4 − Now, print a new line and print the string obtained above on the screen by using fmt.Println() function.

Example

In this example are going to use TrimLeft() function of Golang −

package main
import (
   "fmt"
   "strings"
)

func main() {
   var str string = "a this is a string a"
   fmt.Println("The given string is:\n", str)
   var trimmed string
   trimmed = strings.TrimLeft(str, "a")
   fmt.Println()
   fmt.Println("The string obtained after trimming the above string from both sides is:\n", trimmed)
}

Output

The given string is:
 a this is a string a

The string obtained after trimming the above string from both sides is:
  this is a string a

Example 2

In this example we will write a go language program to trim a string from the left by using TrimPrefix() function present in strings package.

package main
import (
   "fmt"
   "strings"
)
func main() {
   var str string = " Hello, world "
   fmt.Println("The given string is:\n", str)
   var trimmed string
   var prefix string = " Hello, "
   trimmed = strings.TrimPrefix(str, prefix)
   fmt.Println()
   fmt.Println("The string obtained after trimming the above string from the left is:\n", trimmed)
}

Output

The given string is:
  Hello, world 

The string obtained after trimming the above string from the left is:
 world 

Conclusion

We have successfully compiled and executed a Golang Program to trim a string from the left side along with examples. we have written three examples. we have written three programs in this article. In the first example we are using a for loop to remove the elements from the string and in the other two examples we are using inbuilt library functions present in strings package called TrimLeft() and TrimPrefix() in order to perform the respective result.

Updated on: 17-Feb-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements