Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - String previous() method



previous() method is called by the -- operator for the class String. It decrements the last character in the given String.

Syntax

String previous()

Parameters

NA

Return Value

The new value of the string.

Example - Use of previous() method on String objects

Following is an example of the usage of previous() method.

Example.groovy

class Example { 
   static void main(String[] args) { 
      String a = "Hello World"; 
      println(a.previous());         
   } 
}

Output

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

Hello Worlc

Example - Use of previous() method on String literal

Following is an example of the usage of previous() method.

Example.groovy

class Example { 
   static void main(String[] args) { 
      println("Hello World".previous());         
   } 
}

Output

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

Hello Worlc

Example - Use of previous() method with Single Char String

Following is an example of the usage of previous() method.

Example.groovy

class Example { 
   static void main(String[] args) { 	      
      println("B".previous());
	  println("b".previous());
   } 
}

Output

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

A
a
groovy_strings.htm
Advertisements