English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Golang basic tutorials

Golang control statements

Golang functions & methods

Golang structs

Golang slices & arrays

Golang strings (String)

Golang pointers

Golang interfaces

Golang concurrency

Golang exceptions (Error)

Golang other items

Go string trimming, methods to remove leading and trailing spaces

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

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

Syntax:

func Trim(str string, cutstr string) string

Here,strRepresents the current string, whilecutstrindicating the elements to be trimmed from the given string.

package main
import (
    "fmt"
    "strings"
)
func main() {
    //Creating and initializing string
    //Using shorthand declaration
    str1 := "!!Welcome to w3codebox !!"
    str2 := "@@This is the tutorial of Golang$$"
    //Display string
    fmt.Println("Original string:")
    fmt.Println("String" 1: "1)
    fmt.Println("String" 2str2)
    //Trim the given string
    // Use the Trim() function
    res1 := strings.Trim(str1It is a string representation of a character (\"!\" is an example).
    res2 := strings.Trim(str2, "@$")
    // Display result
    fmt.Println("\nTrimmed string:")
    fmt.Println("Result" 1: "1)
    fmt.Println("Result" 2res2)
}

Output:

Original string:
String 1: !!Welcome to w3codebox !!
String 2: @@This is the tutorial of Golang$$
Trimmed string:
Result 1:  Welcome to w3codebox
Result 2: This is the tutorial of Golang

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

Syntax:

func TrimLeft(str string, cutstr string) string

Here,strRepresents the current string, whilecutstrIndicates the left elements to be trimmed from the given string.

//Trim elements from the left of the string
package main
import (
    "fmt"
    "strings"
)
func main() {
    //Creating and initializing string
    //Using shorthand declaration
    str1 := "!!Welcome to w3codebox **"
    str2 := "@@This is the tutorial of Golang$$"
    // Display string
    fmt.Println("Original string:")
    fmt.Println("String" 1: "1)
    fmt.Println("String" 2str2)
    // Trim the given string
    // Using TrimLeft() function
    res1 := strings.TrimLeft(str1, "!*","
    res2 := strings.TrimLeft(str2, "@")
    fmt.Println("\nTrimmed string:")
    fmt.Println("Result" 1: "1)
    fmt.Println("Result" 2res2)
}

Output:

Original string:
String 1: !!Welcome to w3codebox **
String 2: @@This is the tutorial of Golang$$
Trimmed string:
Result 1:  Welcome to w3codebox **
Result 2: This is the tutorial of Golang$$

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

Syntax:

func TrimRight(str string, cutstr string) string

Here,strRepresents the current string, whilecutstrIndicates the right elements to be trimmed from the given string.

//Trim elements from the right of the string
package main
import (
    "fmt"
    "strings"
)
func main() {
    //Create and initialize
    //Using shorthand declaration of string
    str1 := "!!Welcome to w3codebox **"
    str2 := "@@This is the tutorial of Golang$$"
    // Display string
    fmt.Println("Original string:")
    fmt.Println("String" 1: "1)
    fmt.Println("String" 2str2)
    //Trim the given string
    // Using TrimRight() function
    res1 := strings.TrimRight(str1, "!*","
    res2 := strings.TrimRight(str2, "$")
    fmt.Println("\nTrimmed string:")
    fmt.Println("Result" 1: "1)
    fmt.Println("Result" 2res2)
}

Output:

Original string:
String 1: !!Welcome to w3codebox **
String 2: @@This is the tutorial of Golang$$
Trimmed string:
Result 1: !!Welcome to w3codebox
Result 2: @@This is the tutorial of Golang

4. TrimSpace:This function is used to trim all leading and trailing whitespaces (spaces) from the specified string.

Syntax:

func TrimSpace(str string) string
//Delete spaces at both ends of the string
package main
import (
    "fmt"
    "strings"
)
func main() {
    //Creating and initializing string
    //Using shorthand declaration
    str1 := "   **Welcome to w3codebox**   "
    str2 := "  ##This is the tutorial of Golang##  "
    //Display string
    fmt.Println("String trimming before:")
    fmt.Println(str1, str2)
    //Trim spaces from the given string
    //Using TrimSpace() function
    res1 := strings.TrimSpace(str1)
    res2 := strings.TrimSpace(str2)
    // Display result
    fmt.Println("\nString trimming after:")
    fmt.Println(res1, res2)
}

Output:

String trimming before:
   **Welcome to w3codebox**      ##This is the tutorial of Golang##
String trimming after:
**Welcome to w3codebox** ##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, this function will return the original string without any changes.

Syntax:

func TrimSuffix(str, suffstr string) string

Here,strRepresents the original string,suffstrRepresents the suffix string.

//Trim suffix string
//Given string
package main
import (
    "fmt"
    "strings"
)
func main() {
    //Creating and initializing string
    //Using shorthand declaration
    str1 := "Welcome, w"3codebox"
    str2 := "This is the tutorial of Golang"
    //Display string
    fmt.Println("String trimming before:")
    fmt.Println("String" 1: "1)
    fmt.Println("String" 2str2)
    //Trim suffix string from the given string
    //Using TrimSuffix() function
    res1 := strings.TrimSuffix(str1, "w"3codebox")
    res2 := strings.TrimSuffix(str2, "Hello"
    //Display result
    fmt.Println("\nString trimming after:")
    fmt.Println("Result" 1: "1)
    fmt.Println("Result" 2res2)
}

Output:

String trimming before:
String 1Welcome, w3codebox
String 2This is the tutorial of Golang
String trimming after:
Result 1: "Welcome,"
Result 2This 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, this function will return the original string without any changes.

Syntax:

func TrimPrefix(str, suffstr string) string

Here,strRepresents the original string,suffstrRepresents the prefix string.

//Trim prefix string from
//Given string
package main 
  
import ( 
    "fmt"
    "strings"
) 
  
func main() { 
  
//Creating and initializing string
//Using shorthand declaration
    str1 := "Welcome, w"3codebox"
    str2 := "This is the tutorial of Golang"
  
    //Display string
    fmt.Println("String trimming before:")
    fmt.Println("String" 1: "1)
    fmt.Println("String" 2str2)
  
//Trim prefix string from the given string
//Using TrimPrefix() function
    res1 := strings.TrimPrefix(str1, "Welcome" 
    res2 := strings.TrimPrefix(str2, "Hello" 
  
    //Display result
    fmt.Println("\nString trimming after:")
    fmt.Println("Result" 1: "1)
    fmt.Println("Result" 2res2)
}

Output:

String trimming before:
String 1Welcome, w3codebox
String 2This is the tutorial of Golang
String trimming after:
Result 1w3codebox
Result 2This is the tutorial of Golang