Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - String length



Syntax

The length of strings are determined using length() method.

String.length() 

Return Value

The return value is the length of the string.

Example - Getting length of a string variable.

Following is an example of the usage of length() method of string in Groovy −

Example.groovy

class Example { 
   static void main(String[] args) { 		
      String text = "Hello";
      println(text.length());
   } 
}

Output

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

5

Example - Getting length of String literal

Following is an example of the usage of length() function on a string literal value in Groovy −

Example.groovy

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

Output

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

5

Example - Getting length of empty strings

Following is an example of the usage of length() function on empty strings in Groovy −

Example.groovy

class Example { 
   static void main(String[] args) { 	
      String text = "";
      println(text.length());
      println("".length());
   } 
}

Output

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

0
0
groovy_strings.htm
Advertisements