GoLang String - TrimLeft() and TrimRight()
In this tutorial, we will learn how to remove specified characters from left side of a string and remove specified characters from right side of a string.
In the Go Language, If you want to remove the specified characters from the left side of the actual_string then TrimLeft() is used. If you want to remove the specified characters from the right side of the actual_string then TrimRight() is used. let us explore the uses cases of each separately with help of simple examples.
String TrimLeft()
TrimLeft() is the method available in strings package which will remove the specified characters from the left side of the actual_string.
Syntax:
strings.TrimLeft(actual_string,sub_string)
It takes two parameters.
Parameters:
actual_string is the string and sub_string is the string which is removed from the left side of the actual_string if it exists.
It is important to specify the "strings" package in import.
Example 1:
Let's consider the string - "welcome to gkindex" and remove "welcome" using TrimLeft().
package main
import (
"fmt"
"strings")
func main() {
// Consider the string
var actual_string = "welcome to gkindex"
fmt.Println("String: ", actual_string)
// remove "welcome".
fmt.Println(strings.TrimLeft(actual_string,"welcome"))
}
Output:
String: welcome to gkindex
to gkindex
Here, we are removing "welcome" from left of the actual_string.
Example 2:
Let's consider the string - "welcome to gkindex" and remove "welcome to gk" using TrimLeft().
package main
import (
"fmt"
"strings")
func main() {
// Consider the string
var actual_string = "welcome to gkindex"
fmt.Println("String: ", actual_string)
// remove "welcome to gk".
fmt.Println(strings.TrimLeft(actual_string,"welcome to gk"))
}
Output:
String: welcome to gkindex
index
Here, we are removing "welcome to gk" from left of the actual_string. so, the final string is - "index".
Example 3:
Let's consider the string - "welcome to gkindex" and remove "gkindex" using TrimLeft().
package main
import (
"fmt"
"strings")
func main() {
// Consider the string
var actual_string = "welcome to gkindex"
fmt.Println("String: ", actual_string)
// remove "gkindex".
fmt.Println(strings.TrimLeft(actual_string,"gkindex"))
}
Output:
String: welcome to gkindex
welcome to gkindex
Here, we are removing "gkindex" from left of the actual_string. But it is not removed since the "gkindex" is not present at the starting of the string.
String TrimRight()
TrimRight() is the method available in strings package which will remove the specified characters from the right side of the actual_string.
Syntax:
strings.TrimRight(actual_string,sub_string)
It takes two parameters.
Parameters: actual_string is the string and sub_string is the string which is removed from the right side of the actual_string if it exists. It is important to specify the "strings" package in import.
Example 1:
Let's consider the string - "welcome to gkindex" and remove "gkindex" using TrimRight().
package main
import (
"fmt"
"strings")
func main() {
// Consider the string
var actual_string = "welcome to gkindex"
fmt.Println("String: ", actual_string)
// remove "gkindex" from rightside.
fmt.Println(strings.TrimRight(actual_string,"gkindex"))
}
Output:
String: welcome to gkindex
welcome to
Here, we are removing "gkindex" from right side of the actual_string.
Example 2:
Let's consider the string - "welcome to gkindex" and remove "o gkindex" using TrimRight().
package main
import (
"fmt"
"strings")
func main() {
// Consider the string
var actual_string = "welcome to gkindex"
fmt.Println("String: ", actual_string)
// remove "o gkindex" from rightside.
fmt.Println(strings.TrimRight(actual_string,"o gkindex"))
}
Output:
String: welcome to gkindex
welcome t
Here, we are removing "o gkindex" from right of the actual_string. so the final string is - "welcome t".
Example 3:
Let's consider the string - "welcome to gkindex" and remove "welcome" using TrimRight().
package main
import (
"fmt"
"strings")
func main() {
// Consider the string
var actual_string = "welcome to gkindex"
fmt.Println("String: ", actual_string)
// remove "welcome" from rightside.
fmt.Println(strings.TrimRight(actual_string,"welcome"))
}
Output:
String: welcome to gkindex
welcome to gkindex
Here, we are removing "welcome" from right of the actual_string.
But it is not removed since the "welcome" is not present at the right side/ending of the string.
Conclusion
Now we know how to remove the specified characters from the left side of the actual_string using TrimLeft() and from the right side using TrimRight() functions and understood the implementation with working examples.