📜  Java中的扫描器 toString() 方法及示例

📅  最后修改于: 2022-05-13 01:54:54.340000             🧑  作者: Mango

Java中的扫描器 toString() 方法及示例

Java.util.Scanner类的toString()方法返回此 Scanner 的字符串表示形式。确切的格式未指定。

句法:

public String toString()

返回值:此函数返回此扫描仪的字符串表示形式

下面的程序说明了上述函数:

方案一:

// Java program to illustrate the
// toString() method of Scanner class in Java
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        String s = "GeeksForGeeks - "
                   + "A Computer Science Portal for Geeks";
  
        // create a new scanner
        // with the specified String Object
        Scanner scanner = new Scanner(s);
  
        // print a line of the scanner
        System.out.println("Scanner String: \n"
                           + scanner.nextLine());
  
        // display information about this scanner
        System.out.println("\nString representation "
                           + "of this Scanner:\n"
                           + scanner.toString());
  
        // close the scanner
        scanner.close();
    }
}
输出:
Scanner String: 
GeeksForGeeks - A Computer Science Portal for Geeks

String representation of this Scanner:
java.util.Scanner[delimiters=\p{javaWhitespace}+]
[position=51][match valid=true][need input=false]
[skipped=false][group separator=\,]
[decimal separator=\.][positive prefix=]
[negative prefix=\Q-\E][positive suffix=]
[negative suffix=][NaN string=\Q?\E][infinity string=\Q?\E]

参考: https: Java/util/Scanner.html#toString()