Golang Program to trim a string from both sides


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 are going to learn about how to trim a string from both side using Go programming language.

Method 1: By Using A For Loop

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 of the string. 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 variable and assign a value to it. further print the string on the screen.

  • Step 3 − Now, initialize two variables of integer data type and use for loop to iterate over the string. Then compare each character of the string to the first and last character that are to be stored in the string.

  • Step 4 − If the character is found then store its index and break the for loop. Similarly use the same functionality to take the index of the character that is to be removed from the last.

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

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

Example

In this example we have used a for loop to trim a string from both its sides.

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, right int
   for i, c := range str {
      if c != 'a' {
         left = i
         break
      }
   }
   for i := len(str) - 1; i >= 0; i-- {
      if str[i] != 'b' {
         right = i
         break
      }
   }
   trimmed := str[left : right+1]
   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 

Method 2:Using Internal function()

In this method, we are going to use the internal functions – trim() and trimspac() in Golang. Both the function are explained below −

Syntax

func Trim(str string, cutstr string) string

The Trim() function is present in strings package and is used to remove the elements from both sides 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 all the occurrences of the given element.

func TrimSpace(str string) string

TrimSpace() function is present in strings package and is used to remove the leading and trailing white spaces in a string. The function accepts the string whose white spaces are to be removed as arguments and returns a string after removing its white spaces.

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 and assign elements to it. further print the given string on the screen.

  • Step 3 − Then call the Trim() function present in the strings package and pass the string along with the substring which is to be removed.

  • Step 4 − Store the new string obtained in a new variable and print the final string obtained on the screen using fmt.Println() function.

Example 1

In the following example, we are going to use the trim function to to trim a string from both side using go programming language.

package main
import (
   "fmt"
   "strings"
)

func main() {
   var str string = "remove this Hello World! remove this"
   fmt.Println("The given string is:\n", str)
   var trimmed string = strings.Trim(str, "remove this")
   fmt.Println()
   fmt.Println("The string obtained after removing the characters from start and end is:\n", trimmed)
}

Output

The given string is:
 remove this Hello World! remove this

The string obtained after removing the characters from start and end is:
 Hello World!

Example 2

In this example we will write a go language program to trim a string from both sides by using trimspace() function present in strings package. If our program has leading and trailing whitespaces then we can use this function to remove them.

package main
import (
   "fmt"
   "strings"
)

func main() {
   var str string = " 	 Hello World!	  "
   fmt.Println("The given string is:\n", str)
   var trimmed string = strings.TrimSpace(str)
   fmt.Println()
   fmt.Println("The string obtained after removing the leading and trailing white spaces from the above string is:\n", trimmed)
}

Output

The given string is:
  	 Hello World!	  

The string obtained after removing the leading and trailing white spaces from the above string is:
 Hello World!

Conclusion

We have successfully compiled and executed a Golang Program to trim a string from both sides along with examples. Here we have written three programs. In the first example we have used a for loop to remove the elements from start and end of a string. In the second example we are using an inbuilt function present in strings package called Trim() and in the last example we have demonstrated to remove the leading and trailing white spaces from a string by using TrimSpace() library function.

Updated on: 17-Feb-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements