Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   






Trim() FUNCTION


Trim() Function


The Trim() Function is used to remove any unwanted character from the String.


Example :



package main
import "fmt"
import "strings"
    
func main() {
    
    x := "   Hello  "
    y := strings.Trim(x," ")
            
    fmt.Println(y)     	 	
}


Output :



 Hello

In the above code, we have declared a String ' Hello ' with leading and trailing spaces.


x := " Hello "

java_Collections

Then we have used the 'Trim()' function to remove any unwanted spaces from beginning and end.


y := strings.Trim(x," ")

java_Collections

Unwanted spaces are stripped out of it.


Now, let us say you want have asterisk '*' in front of the String and after the String. And you want to remove them.


Example :



package main
import "fmt"
import "strings"
    
func main() {
    
    x := "***Hello**"
    y := strings.Trim(x,"*")
            
    fmt.Println(y)     	 	
}


Output :



 Hello

In the above code, we have declared a String ' Hello ' with leading and trailing asterisk '*'.


x := "***Hello**"

java_Collections

Then we have used the 'Trim()' function to remove any asterisk '*' from beginning and end.


y := strings.Trim(x,"*")

java_Collections

And asterisk '*' are stripped out of it.