Posts

Showing posts with the label Trim

Trim a string in Go Language - Examples

Image
Hello everyone, In this tutorial, we will show you how to trim all of the leading and trailing spaces in a string. Few methods from the strings package are, Trim():  TrimSpace() TrimLeft() TrimRight() TrimPrefix() TrimSuffix() Example 1:  strings.Trim() function The Trim() function is an inbuilt method of the strings package. This method  will remove the leading and trailing whitespace from a string that is received as a parameter. // Golang program to demonstrate the // example of strings.Trim() Method package main import ( "fmt" "strings" ) func main() { str1 := " My world is too small " fmt.Println( "Before:" , str1) str4 := strings.Trim(str1, " " ) fmt.Println( "After:" , str4) } Output: Example 2:  strings.TrimSpace() function The TrimSpace() function is an inbuilt method of the strings package. This is a better choice if a variety of whitespace runes may be present at the start or end of a ...