Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - String substring() method



substring() method returns a new String that is a substring of this string. This method has 2 different variants.

  • String substring(int beginIndex) − The substring starts with the character at the given index and continues to the end of the current string.

Syntax

String substring(int beginIndex)

Parameters

beginIndex − the begin index, inclusive.

Return Value

It returns the required substring

  • String substring(int beginIndex, int endIndex) − The substring contains all the characters from startIndex if endIndex is not specified. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1.

Syntax

public String substring(int beginIndex, int endIndex)

Parameters

beginIndex − the begin index, inclusive.

Return Value

It returns the required substring

Example - Getting the Substring from a String Using Begin Index

Following is an example of the usage of substring() method. Here, only the begin index is passed as a argument to this method −

Example.groovy

class Example { 
   static void main(String[] args) { 
      String str = "This is tutorials point";
      String substr = "";
    
      // prints the substring after index 7 till end of the string
      substr = str.substring(7);
      println("substring = " + substr);

      // prints the substring after index 0 till end of the string
      substr = str.substring(0);
      println("substring = " + substr);       
   } 
}

Output

When we run the above program, we will get the following result −

substring = tutorials point
substring = This is tutorials point

Example - Getting the Substring from a String Using Begin and End Index

Following is an example of the usage of substring() method. Here both the beginning and the ending of the string is passed as parameters −

Example.groovy

class Example { 
   static void main(String[] args) { 
      String str = "This is tutorials point";
      String substr = "";
    
      // prints the substring after index 7 till index 17
      substr = str.substring(7, 17);
      println("substring = " + substr);

      // prints the substring after index 0 till index 7
      substr = str.substring(0, 7);
      println("substring = " + substr);       
   } 
}

Output

When we run the above program, we will get the following result −

substring =  tutorials
substring = This is

Example - Getting the Substring from a String Between Braces

Following is an example of the usage of substring() method. In this example we are trying to retrieve the substring with in the brackets from a string.

Example.groovy

class Example { 
   static void main(String[] args) { 
      String s = "Stabbed in the (back)";
      
      // getting substring between '(' and ')' delimeter
      int begin = s.indexOf("(");
      int ter = s.indexOf(")");
      String res = s.substring(begin + 1, ter);
      System.out.println("The substring is: " + res);      
   } 
}

Output

When we run the above program, we will get the following result −

The substring is: back
groovy_strings.htm
Advertisements