Java中的 ParsePosition toString() 方法与示例
Java.text.ParsePosition类的toString()方法用于检索以字符串形式表示的解析位置对象。
句法:
public String toString()
参数:此方法不接受任何参数作为参数。
返回值:该方法返回以字符串形式表示的解析位置对象。
下面是说明toString()方法的示例:
示例 1:
Java
// Java program to demonstrate
// toString() method
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
try {
// Creating and initializing
// new ParsePosition Object
ParsePosition pos
= new ParsePosition(500);
// setting error index
pos.setErrorIndex(30);
// getting string representation
// of this ParsePosition Object
// using toString() method
String i = pos.toString();
// display result
System.out.println(
"ParsePosition: "
+ i);
}
catch (ClassCastException e) {
System.out.println(e);
}
}
}
Java
// Java program to demonstrate
// toString() method
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
try {
// Creating and initializing
// new ParsePosition Object
ParsePosition pos
= new ParsePosition(-1);
// setting error index
pos.setErrorIndex(3000);
// getting string representation
// of this ParsePosition Object
// using toString() method
String i = pos.toString();
// display result
System.out.println(
"ParsePosition: "
+ i);
}
catch (ClassCastException e) {
System.out.println(e);
}
}
}
输出:
ParsePosition: java.text.ParsePosition[index=500,errorIndex=30]
示例 2:
Java
// Java program to demonstrate
// toString() method
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
try {
// Creating and initializing
// new ParsePosition Object
ParsePosition pos
= new ParsePosition(-1);
// setting error index
pos.setErrorIndex(3000);
// getting string representation
// of this ParsePosition Object
// using toString() method
String i = pos.toString();
// display result
System.out.println(
"ParsePosition: "
+ i);
}
catch (ClassCastException e) {
System.out.println(e);
}
}
}
输出:
ParsePosition: java.text.ParsePosition[index=-1,errorIndex=3000]
参考: https://docs.oracle.com/javase/9/docs/api/ Java/text/ParsePosition.html#toString–