Trim a string in Go Language - Examples

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,
  1. Trim(): 
  2. TrimSpace()
  3. TrimLeft()
  4. TrimRight()
  5. TrimPrefix()
  6. 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 string.For example, '\t', '\n', '\v', '\f', '\r', ' '
// Golang program to demonstrate the
// example of strings.TrimSpace() Method
package main

import (
"fmt"
"strings"
)

func main() {

str1 := " \t My world is too small"
fmt.Println("Before:", str1)

str4 := strings.TrimSpace(str1)
fmt.Println("After:", str4)

}
Output:




Example 3: strings.TrimLeft() function

The TrimLeft() method will remove the leading whitespace from a string that is received as a parameter.
// Golang program to demonstrate the
// example of strings.TrimLeft() Method
package main

import (
"fmt"
"strings"
)

func main() {

str1 := " My world is too small "
fmt.Println("Before:", str1)

str4 := strings.TrimLeft(str1, " ")
fmt.Println("After:", str4)

}
Output:




Example 4: strings.TrimRight() function

The TrimRight() method will remove the trailing whitespace from a string that is received as a parameter.
// Golang program to demonstrate the
// example of strings.TrimRight() Method
package main

import (
"fmt"
"strings"
)

func main() {

str1 := " My world is too small "
fmt.Println("Before:", str1)

str4 := strings.TrimRight(str1, " ")
fmt.Println("After:", str4)

}
Output:




Example 5: strings.TrimPrefix() function

The TrimPrefix() method will remove the prefix from a string that is received as a parameter.
// Golang program to demonstrate the
// example of strings.TrimPrefix() Method
package main

import (
"fmt"
"strings"
)

func main() {

str1 := "http://knowledgefactory.net"
fmt.Println("Before:", str1)

str4 := strings.TrimPrefix(str1, "http://")
fmt.Println("After:", str4)

}
Output:




Example 6: strings.TrimSuffix() function

The TrimSuffix() method will remove the suffix from a string that is received as a parameter.
// Golang program to demonstrate the
// example of strings.TrimSuffix() Method
package main

import (
"fmt"
"strings"
)

func main() {

str1 := "http://knowledgefactory.net.com"
fmt.Println("Before:", str1)

str4 := strings.TrimSuffix(str1, ".com")
fmt.Println("After:", str4)

}
Output:

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete