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






HasPrefix() FUNCTION


HasPrefix() Function


The HasPrefix() Function is used to check if a String begins with a given set of Characters.


Example :



package main
import "fmt"
import "strings"
    
func main() {
    
    x := "Hello Beautiful World"
    y := strings.HasPrefix(x, "Hel")
    if (y == true) { 
        fmt.Println("The String begins with the sub string Hel")  
    } else {
        fmt.Println("The String does not begins with the sub string Hel")
    }		
}	


Output :



 The String begins with the sub string Hel

In the above code, we have declared a String 'Hello Beautiful World' and assigned it to a variable 'x'.


x := "Hello Beautiful World"

java_Collections

And we would be checking if the String 'Hello Beautiful World' starts with 'Hel'.


So, we have used the 'HasPrefix()' Function to check.


y := strings.HasPrefix(x, "Hel")

And what 'HasPrefix()' Function does is, goes to the String 'Hello Beautiful World' and searches if the String begins with 'Hel'.


java_Collections

And finds that the String 'Hello Beautiful World' begins with 'Hel'.


So the control gets into the 'if' block and the below output is printed.


The String begins with the sub string Hel