Substring in Java

    Substring in Java

    A substring is considered to be a subset of another string. The startIndex of the substring is inclusive, and the endIndex of the substring is exclusive. The index starts from zero. You can work on the substring of the string by using the below methods-

    • public String substring(int startIndex): This method will return the new String object that will contain the substring of the given string from the specified startIndex (inclusive).
    • public String substring(int startIndex, int endIndex): This method will return the new String object that will contain the substring of the given string from specified startIndex to specified endIndex.

    Example-

    class Simple{  
    
     public static void main(String args[]){  
    
    String s1="Welcome Here";
    
    
    
    System.out.println(s1.substring(5)); 
    
      System.out.println(s1.substring(0,5));
    
     }  
    
    } 

    Output-