ParsePosition toString() method in Java with Example
The toString()
method of the ParsePosition
class in Java returns a string representation of the current state of the ParsePosition
object. It returns a string that contains the index of the current parse position and the error index.
Syntax:
public String toString()
Example:
import java.text.ParsePosition;
public class ParsePositionToStringExample {
public static void main(String[] args) {
String input = "Hello, World!";
ParsePosition position = new ParsePosition(7);
int index = position.getIndex();
int errorIndex = position.getErrorIndex();
System.out.println("Index: " + index);
System.out.println("Error Index: " + errorIndex);
System.out.println("Parse Position: " + position.toString());
}
}
Output:
Index: 7
Error Index: -1
Parse Position: java.text.ParsePosition[index=7,errorIndex=-1]
In the above example, we have created a ParsePosition
object with an initial index of 7. We have then retrieved the index and error index using the getIndex()
and getErrorIndex()
methods respectively. Finally, we have printed the string representation of the ParsePosition
object using the toString()
method. The output shows the index and error index values along with the class name and the values in square brackets.