![]() |
VOOZH | about |
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!!"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
Table of Content
The Trim function removes all specified leading and trailing characters from a string.
func Trim(s string, cutset string) strings: The string to trim.cutset: Characters to remove from both ends.Example:
Hello, Geeks
The TrimLeft function removes specified characters from the start of a string.
func TrimLeft(s string, cutset string) stringExample:
Hello, Geeks!!
The TrimRight function removes specified characters from the end of a string.
func TrimRight(s string, cutset string) stringExample
@@Hello,
The TrimSpace function removes all leading and trailing whitespace from a string.
func TrimSpace(s string) stringExample:
Hello, Geeks
The TrimPrefix function removes a specified prefix from the beginning of a string if present.
func TrimPrefix(s, prefix string) stringExample:
Hello, Geeks!!
The TrimSuffix function removes a specified suffix from the end of a string if present.
@@Hello, G