Thursday, December 26, 2024
Google search engine
HomeLanguagesGolangHow to Trim a String in Golang?

How to Trim a String in Golang?

In Go language, strings are different from other languages like Java, C++, Python, etc. It is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding. You can trim a string in different ways using the following list of functions. All these functions are defined under strings package, so you have to import strings package in your program to access these functions.

1. Trim: This function is used to trim the string all the leading and trailing Unicode code points which are specified in this function.

Syntax:

func Trim(str string, cutstr string) string

Here, str represent the current string and cutstr represents the elements which you want to trim in the given string.

Example:




// Go program to illustrate 
// how to trim a string
package main
  
import (
    "fmt"
    "strings"
)
  
// Main method
func main() {
  
    // Creating and initializing string
    // Using shorthand declaration
    str1 := "!!Welcome to GeeksforGeeks !!"
    str2 := "@@This is the tutorial of Golang$$"
  
    // Displaying strings
    fmt.Println("Strings before trimming:")
    fmt.Println("String 1: ", str1)
    fmt.Println("String 2:", str2)
  
    // Trimming the given strings
    // Using Trim() function
    res1 := strings.Trim(str1, "!")
    res2 := strings.Trim(str2, "@$")
  
    // Displaying the results
    fmt.Println("\nStrings after trimming:")
    fmt.Println("Result 1: ", res1)
    fmt.Println("Result 2:", res2)
}


Output:

Strings before trimming:
String 1:  !!Welcome to GeeksforGeeks !!
String 2: @@This is the tutorial of Golang$$

Strings after trimming:
Result 1:  Welcome to GeeksforGeeks 
Result 2: This is the tutorial of Golang

 

2. TrimLeft: This function is used to trim the left-hand side(specified in the function) Unicode code points of the string.

Syntax:

func TrimLeft(str string, cutstr string) string

Here, str represent the current string and cutstr represents the left-hand side elements which you want to trim in the given string.

Example:




// Go program to illustrate how to
// trim left-hand side elements
// from the string
package main
  
import (
    "fmt"
    "strings"
)
  
// Main method
func main() {
  
    // Creating and initializing string
    // Using shorthand declaration
    str1 := "!!Welcome to GeeksforGeeks **"
    str2 := "@@This is the tutorial of Golang$$"
  
    // Displaying strings
    fmt.Println("Strings before trimming:")
    fmt.Println("String 1: ", str1)
    fmt.Println("String 2:", str2)
  
    // Trimming the given strings
    // Using TrimLeft() function
    res1 := strings.TrimLeft(str1, "!*")
    res2 := strings.TrimLeft(str2, "@")
  
    // Displaying the results
    fmt.Println("\nStrings after trimming:")
    fmt.Println("Result 1: ", res1)
    fmt.Println("Result 2:", res2)
}


Output:

Strings before trimming:
String 1:  !!Welcome to GeeksforGeeks **
String 2: @@This is the tutorial of Golang$$

Strings after trimming:
Result 1:  Welcome to GeeksforGeeks **
Result 2: This is the tutorial of Golang$$

 

3. TrimRight: This function is used to trim the right-hand side(specified in the function) Unicode code points of the string.

Syntax:

func TrimRight(str string, cutstr string) string

Here, str represent the current string and cutstr represents the right-hand side elements which you want to trim in the given string.

Example:




// Go program to illustrate how to
// trim right-hand side elements
// from the string
package main
  
import (
    "fmt"
    "strings"
)
  
// Main method
func main() {
  
    // Creating and initializing the
    // string using shorthand declaration
    str1 := "!!Welcome to GeeksforGeeks **"
    str2 := "@@This is the tutorial of Golang$$"
  
    // Displaying strings
    fmt.Println("Strings before trimming:")
    fmt.Println("String 1: ", str1)
    fmt.Println("String 2:", str2)
  
    // Trimming the given strings
    // Using TrimRight() function
    res1 := strings.TrimRight(str1, "!*")
    res2 := strings.TrimRight(str2, "$")
  
    // Displaying the results
    fmt.Println("\nStrings after trimming:")
    fmt.Println("Result 1: ", res1)
    fmt.Println("Result 2:", res2)
}


Output:

Strings before trimming:
String 1:  !!Welcome to GeeksforGeeks **
String 2: @@This is the tutorial of Golang$$

Strings after trimming:
Result 1:  !!Welcome to GeeksforGeeks 
Result 2: @@This is the tutorial of Golang

 

4. TrimSpace: This function is used to trim all the leading and trailing white space from the specified string.

Syntax:

func TrimSpace(str string) string

Example:




// Go program to illustrate how to
// trim white space from the string
package main
  
import (
    "fmt"
    "strings"
)
  
// Main method
func main() {
  
    // Creating and initializing string
    // Using shorthand declaration
    str1 := "   **Welcome to GeeksforGeeks**   "
    str2 := "  ##This is the tutorial of Golang##  "
  
    // Displaying strings
    fmt.Println("Strings before trimming:")
    fmt.Println(str1, str2)
  
    // Trimming white space from the given strings
    // Using TrimSpace() function
    res1 := strings.TrimSpace(str1)
    res2 := strings.TrimSpace(str2)
  
    // Displaying the results
    fmt.Println("\nStrings after trimming:")
    fmt.Println(res1, res2)
}


Output:

Strings before trimming:
   **Welcome to GeeksforGeeks**      ##This is the tutorial of Golang##  

Strings after trimming:
**Welcome to GeeksforGeeks** ##This is the tutorial of Golang##

 

5. TrimSuffix: This method is used to trim the trailing suffix string from the given string. If the given string does not contain the specified suffix string, then this function returns the original string without any change.

Syntax:

func TrimSuffix(str, suffstr string) string

Here, str represents the original string and suffstr represent the suffix string.

Example:




// Go program to illustrate how to
// trim a suffix string from the
// given string
package main
  
import (
    "fmt"
    "strings"
)
  
// Main method
func main() {
  
    // Creating and initializing string
    // Using shorthand declaration
    str1 := "Welcome, GeeksforGeeks"
    str2 := "This is the, tutorial of Golang"
  
    // Displaying strings
    fmt.Println("Strings before trimming:")
    fmt.Println("String 1: ", str1)
    fmt.Println("String 2:", str2)
  
    // Trimming suffix string from the given strings
    // Using TrimSuffix() function
    res1 := strings.TrimSuffix(str1, "GeeksforGeeks")
    res2 := strings.TrimSuffix(str2, "Hello")
  
    // Displaying the results
    fmt.Println("\nStrings after trimming:")
    fmt.Println("Result 1: ", res1)
    fmt.Println("Result 2:", res2)
}


Output:

Strings before trimming:
String 1:  Welcome, GeeksforGeeks
String 2: This is the, tutorial of Golang

Strings after trimming:
Result 1:  Welcome, 
Result 2: This is the, tutorial of Golang

 

6. TrimPrefix: This method is used to trim the leading prefix string from the given string. If the given string does not contain the specified prefix string, then this function returns the original string without any change.

Syntax:

func TrimPrefix(str, suffstr string) string

Here, str represents the original string and suffstr represent the prefix string.

Example:




// Go program to illustrate how to
// trim the prefix string from the
// given string
package main
  
import (
    "fmt"
    "strings"
)
  
// Main method
func main() {
  
    // Creating and initializing string
    // Using shorthand declaration
    str1 := "Welcome, GeeksforGeeks"
    str2 := "This is the, tutorial of Golang"
  
    // Displaying strings
    fmt.Println("Strings before trimming:")
    fmt.Println("String 1: ", str1)
    fmt.Println("String 2: ", str2)
  
    // Trimming prefix string from the given strings
    // Using TrimPrefix() function
    res1 := strings.TrimPrefix(str1, "Welcome")
    res2 := strings.TrimPrefix(str2, "Hello")
  
    // Displaying the results
    fmt.Println("\nStrings after trimming:")
    fmt.Println("Result 1: ", res1)
    fmt.Println("Result 2: ", res2)
}


Output:

Strings before trimming:
String 1:  Welcome, GeeksforGeeks
String 2:  This is the, tutorial of Golang

Strings after trimming:
Result 1:  , GeeksforGeeks
Result 2:  This is the, tutorial of Golang

RELATED ARTICLES

Most Popular

Recent Comments