Golang Program to trim a string from the right side


Strings are a built-in data type that represents sequences of characters in Golang. 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 right side. We will achieve this both by using for loop and inbuilt library functions present in go language.

Method 1: Using User-defined function

In this method, the for loop is used to iterate over the string and then it compares the character of the string to the character from the right 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 right 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 right 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 right 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 right int
   for i := len(str) - 1; i >= 0; i-- {
      if str[i] != 'b' {
         right = i
         break
      }
   }

   trimmed := str[:right]
   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:
 a this is a string

Method 2: Using internal function

In this method, we are going to use the internal function of Trim function in Golang – TrimRight and Trimsuffix. These syntax are explained below −

Syntax

func TrimRight(str string, cutstr string) string

The TrimRight() function is present in strings package and is used to remove the elements from right 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 right.

func TrimSuffix(str, suffix string) string

TrimSuffix() function is an inbuilt function in go language present in strings package and is used to remove a particular suffix 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 suffix 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 TrimRight() 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 1

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

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.TrimRight(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:
 a this is a string 

Example 2

In this example we will write a go language program to trim a string from the right by using TrimSuffix() 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 suffix string = " world."
   trimmed = strings.TrimSuffix(str, suffix)
   fmt.Println()
   fmt.Println("The string obtained after trimming the above string from the right is:\n", trimmed)
}

Output

The given string is:
 Hello, world.

The string obtained after trimming the above string from the right is:
 Hello,

Conclusion

We have successfully compiled and executed a Golang Program to trim a string from the right 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 TrimRight() and TrimSuffix() in order to perform the respective result.

Updated on: 17-Feb-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements