Groovy – subString()

  • Post author:
  • Post category:Groovy
  • Post comments:0 Comments
substrings

Returns a new String that is a substrings of this String. This method has 2 different variants

  • String substring(int beginIndex) โˆ’ Pad the String with the spaces appended to the right.

Syntax

String substring(int beginIndex)

Parameters

  • beginIndex โˆ’ the begin index, inclusive.

Return Value โˆ’ The specified substring.

  • String substring(int beginIndex, int endIndex) โˆ’ Pad the String with the padding characters appended to the right.

Syntax

String substring(int beginIndex, int endIndex)

Parameters

  • beginIndex โˆ’ the begin index, inclusive.
  • endIndex โˆ’ the end index, exclusive.

Return Value โˆ’ The specified substring.

Example

Following is an example of the usage of both variants โˆ’

class Example { 
   static void main(String[] args) { 
      String a = "HelloWorld"; 
      println(a.substring(4)); 
      println(a.substring(4,8));
   }
}

When we run the above program, we will get the following result โˆ’

oWorld 
oWor

Previous Page:-Click Here

Leave a Reply