[go: up one dir, main page]

Open In App

How to Trim a String in Golang?

Last Updated : 05 Nov, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In Go, strings are UTF-8 encoded sequences of variable-width characters, unlike some other languages like Java, python and C++. Go provides several functions within the strings package to trim characters from strings.In this article we will learn how to Trim a String in Golang.

Example

s := "@@Hello, Geeks!!"

Syntax

func Trim(s string, cutset string) string                        #Trim
func TrimLeft(s string, cutset string) string # TrimLeft
func TrimRight(s string, cutset string) string # TrimRight
func TrimSpace(s string) string # TrimSpace
func TrimPrefix(s, prefix string) string # TrimPrefix
func TrimSuffix(s, suffix string) string # TrimSuffix

Trim

The Trim function removes all specified leading and trailing characters from a string.

Syntax

func Trim(s string, cutset string) string
  • s: The string to trim.
  • cutset: Characters to remove from both ends.

Example:

Go
package main 

import (
    "fmt"
    "strings"
)

func main() {
    s := "@@Hello, Geeks!!"
    result := strings.Trim(s, "@!")
    fmt.Println(result)
}

Output
Hello, Geeks

TrimLeft

The TrimLeft function removes specified characters from the start of a string.

Syntax

func TrimLeft(s string, cutset string) string

Example:

Go
package main

import (
    "fmt"
    "strings"
)

func main() {
    s := "@@Hello, Geeks!!"
    result := strings.TrimLeft(s, "@!")
    fmt.Println(result)
}

Output
Hello, Geeks!!

TrimRight

The TrimRight function removes specified characters from the end of a string.

Syntax

func TrimRight(s string, cutset string) string

Example

Go
package main

import (
    "fmt"
    "strings"
)

func main() {
    s := "@@Hello, Geeks"
    result := strings.TrimRight(s, "Geeks")
    fmt.Println(result)
}

Output
@@Hello, 

TrimSpace

The TrimSpace function removes all leading and trailing whitespace from a string.

Syntax

func TrimSpace(s string) string

Example:

Go
package main
import (
    "fmt"
    "strings"
)

func main() {
    s := "   Hello, Geeks   "
    result := strings.TrimSpace(s)
    fmt.Println(result)
}

Output
Hello, Geeks

TrimPrefix

The TrimPrefix function removes a specified prefix from the beginning of a string if present.

Syntax

func TrimPrefix(s, prefix string) string

Example:

Go
package main
import (
    "fmt"
    "strings"
)

func main() {
    s := "@@Hello, Geeks!!"
    result := strings.TrimPrefix(s, "@@")
    fmt.Println(result)
}

Output
Hello, Geeks!!

TrimSuffix

The TrimSuffix function removes a specified suffix from the end of a string if present.

Syntax

Go
package main
import (
    "fmt"
    "strings"
)
func main() {
    s := "@@Hello, Geeks"
    result := strings.TrimSuffix(s, "eeks")
    fmt.Println(result)
}

Output
@@Hello, G



Previous Article
Next Article

Similar Reads

How to trim prefix from the slice of bytes in Golang?
In Go language slice is more powerful, flexible, convenient than an array, and is a lightweight data structure. The slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of elements in the same slice. In the Go slice of bytes, you are allowed to trim prefix from the given slice usin
3 min read
How to trim suffix from the slice of bytes in Golang?
In Go language slice is more powerful, flexible, convenient than an array, and is a lightweight data structure. The slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of elements in the same slice. In the Go slice of bytes, you are allowed to trim suffix from the given slice usin
3 min read
How to trim a slice of bytes in Golang?
In Go language slice is more powerful, flexible, convenient than an array, and is a lightweight data structure. The slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of elements in the same slice. In the Go slice of bytes, you are allowed to trim all the leading and trailing UTF
4 min read
How to trim white spaces from the slice of bytes in Golang?
In Go language slice is more powerful, flexible, convenient than an array, and is a lightweight data structure. The slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of elements in the same slice. In the Go slice of bytes, you are allowed to trim all the leading and trailing whi
3 min read
How to trim left-hand side of a slice of bytes in Golang?
In Go language slice is more powerful, flexible, convenient than an array, and is a lightweight data structure. The slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of elements in the same slice. In the Go slice of bytes, you are allowed to trim all the starting UTF-8-encoded c
3 min read
How to trim right-hand side of a slice of bytes in Golang?
In Go language slice is more powerful, flexible, convenient than an array, and is a lightweight data structure. The slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of elements in the same slice. In the Go slice of bytes, you are allowed to trim all the ending UTF-8-encoded cod
3 min read
Converting a string variable into Boolean, Integer or Float type in Golang
Various types of string conversions are needed while performing tasks in Golang. Package strconv is imported to perform conversions to and from string. String to Boolean Conversion ParseBool is used to convert string to boolean value. It accepts 1, t, T, TRUE, true, True as true and 0, f, F, FALSE, false, False as false. Any other value returns an
3 min read
Check if the given characters is present in Golang String
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. In Go strings, you are allowed to check the given characters present in the string using the given functions. These functions a
3 min read
How to Replace Characters in Golang String?
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. In Go strings, you are allowed to replace characters in the given string using the given functions. These functions are defined
4 min read
Repeating a String for Specific Number of Times 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 are allowed to repeat a string of for a specific number of times with the help of the Repeat() Function. This method return
3 min read
How to find the index value of specified 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. In Go strings, you can find the first index value of the specified string from the original string using the following function
4 min read
How to find the last index value of specified 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. In Go strings, you can find the last index value of the specified string from the original string using the following functions
4 min read
How to Count the Number of Repeated Characters in a Golang String?
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. In Go string, you can count some specific Unicode code points or the number of non-overlapping instances of costr (Repeated Cha
2 min read
Check if the String starts with specified prefix 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. In Golang strings, you can check whether the string begins with the specified prefix or not with the help of HasPrefix() functi
2 min read
Check if the String ends with specified suffix 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. In Go strings, you can check whether the string ends with the specified suffix or not with the help of HasSuffix() function. Th
2 min read
How to check String for equality under Unicode case folding 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. In Go strings, you are allowed to compare two strings with each other without any error even if they are written in different c
3 min read
Golang | How to find the index of rune in the string?
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. In the Go strings, you can also find the first index of the specified rune in the given string using IndexRune() function. This
3 min read
How to check the specified rune in Golang String?
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. In the Go strings, you are allowed to check the given string contain the specified rune in it using ContainsRune() function. Th
3 min read
Searching an element of string type in Golang slice
In Go language slice is more powerful, flexible, convenient than an array, and is a lightweight data structure. The slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of elements in the same slice. In the Go slice, you can search an element of string type in the given slice of st
3 min read
Golang | Checking the string for the specified regular expression
A regular expression is a sequence of characters which define a search pattern. Go language support regular expressions. A regular expression is used for parsing, filtering, validating, and extracting meaningful information from large text, like logs, the output generated from other programs, etc. In Go regexp, you are allowed to check whether the
3 min read
Golang | Splitting the string after the specified separator
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. In the Go strings, you are allowed to split the string after the specified separator using a SplitN() function. This function s
3 min read
Golang | Creating a string that contains regexp metacharacters
A regular expression is a sequence of characters which define a search pattern. Go language support regular expressions. A regular expression is used for parsing, filtering, validating, and extracting meaningful information from large text, like logs, the output generated from other programs, etc. In Go regexp, you are allowed to create a string th
2 min read
Golang | Extracting a Regular Expression from the String
A regular expression is a sequence of characters which define a search pattern. Go language support regular expressions. A regular expression is used for parsing, filtering, validating, and extracting meaningful information from large text, like logs, the output generated from other programs, etc. In Go regexp, you are allowed to extract a regular
2 min read
Golang | Extracting all the Regular Expression from the String
A regular expression is a sequence of characters which define a search pattern. Go language support regular expressions. A regular expression is used for parsing, filtering, validating, and extracting meaningful information from large text, like logs, the output generated from other programs, etc. In Go regexp, you are allowed to find all the regul
2 min read
Golang | Finding Index of the Regular Expression present in String
A regular expression is a sequence of characters which define a search pattern. Go language support regular expressions. A regular expression is used for parsing, filtering, validating, and extracting meaningful information from large text, like logs, the output generated from other programs, etc. In Go regexp, you are allowed to find the leftmost
2 min read
Golang | Replacing all String which Matches with Regular Expression
A regular expression is a sequence of characters which define a search pattern. Go language support regular expressions. A regular expression is used for parsing, filtering, validating, and extracting meaningful information from large text, like logs, the output generated from other programs, etc. In Go regexp, you are allowed to replace original s
2 min read
time.String() Function in Golang With Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The String() function in Go language is used to find a string that represents the duration formats as "24h6m0.7s". Here, the foremost zero units are deleted. And the stated duration with less than one-second format will use a smaller unit, for example, mil
2 min read
time.Month.String() Function in Golang With Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The Month.String() function in Go language is used to find the English name of the month. Moreover, this function is defined under the time package. Here, you need to import the "time" package in order to use these functions. Syntax: func (m Month) String(
2 min read
time.Weekday.String() Function in Golang With Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The Weekday.String() function in Go language is used to find the English name of the day. Moreover, this function is defined under the time package. Here, you need to import the "time" package in order to use these functions. Syntax: func (d Weekday) Strin
1 min read
time.Location.String() Function in Golang With Examples
In Go language, time packages supplies functionality for determining as well as viewing time. The Location.String() function in Go language is used to find an explanatory name stated for the data of time zone which is equivalent to the name parameter passes to the LoadLocation or FixedZone method. Moreover, this function is defined under the time p
2 min read
three90RightbarBannerImg