當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Scanner toString()用法及代碼示例


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

用法:

public String toString()

返回值:此函數返回此掃描器的字符串表示形式。


以下示例程序旨在說明上述函數:

示例1:

// 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://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#toString()



相關用法


注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 Scanner toString() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。