Java Scanner findInLine() 方法



描述

Java Scanner findInLine(Pattern pattern) 方法嘗試查詢指定模式的下一個出現,忽略分隔符。如果在下一個行分隔符之前找到該模式,掃描器將跳過匹配的輸入並返回與該模式匹配的字串。如果在輸入中直到下一個行分隔符之前未檢測到此類模式,則返回 null,並且掃描器的位置不變。此方法可能會阻塞,等待與模式匹配的輸入。

宣告

以下是 java.util.Scanner.findInLine() 方法的宣告

public String findInLine(Pattern pattern)

引數

pattern − 要掃描的模式

返回值

此方法返回與指定模式匹配的文字。

異常

IllegalStateException − 如果此掃描器已關閉

Java Scanner findInLine(String pattern) 方法

描述

Java Scanner findInLine(String pattern) 方法嘗試查詢從指定字串構造的模式的下一個出現,忽略分隔符。

宣告

以下是 java.util.Scanner.findInLine(String pattern) 方法的宣告

public String findInLine(String pattern)

引數

pattern − 指定要搜尋的模式的字串

返回值

此方法返回與指定模式匹配的文字。

異常

IllegalStateException − 如果此掃描器已關閉

使用 Scanner 示例在行內查詢模式

以下示例演示了 Java Scanner findInLine(Pattern pattern) 方法的使用,以查詢給定字串中模式的下一個出現。我們使用給定字串建立了一個掃描器物件。我們使用 findInLine() 方法列印包含給定模式的單詞。然後,我們使用 scanner.nextLine() 方法列印字串。

package com.tutorialspoint;

import java.util.Scanner;
import java.util.regex.Pattern;

public class ScannerDemo {
   public static void main(String[] args) {

      String s = "Hello World! 3 + 3.0 = 6";

      // create a new scanner with the specified String Object
      Scanner scanner = new Scanner(s);

      // find a pattern of any letter plus "ello"
      System.out.println(scanner.findInLine(Pattern.compile(".ello")));

      // print the next line of the string
      System.out.println(scanner.nextLine());

      // close the scanner
      scanner.close();
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Hello
 World! 3 + 3.0 = 6

使用 Scanner 示例在行內查詢字串

以下示例演示了 Java Scanner findInLine(String pattern) 方法的使用,以查詢給定字串中模式的下一個出現。我們使用給定字串建立了一個掃描器物件。我們使用 findInLine() 方法列印包含給定模式的單詞。然後,我們使用 scanner.nextLine() 方法列印字串。

package com.tutorialspoint;

import java.util.Scanner;

public class ScannerDemo {
   public static void main(String[] args) {

      String s = "Hello World! 3 + 3.0 = 6";

      // create a new scanner with the specified String Object
      Scanner scanner = new Scanner(s);

      // find a string "World"
      System.out.println(scanner.findInLine("World"));

      // print the next line of the string
      System.out.println(scanner.nextLine());

      // close the scanner
      scanner.close();
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

World
! 3 + 3.0 = 6

使用 Scanner 對使用者輸入在行內查詢模式的示例

以下示例演示了 Java Scanner findInLine(String pattern) 方法的使用,以查詢輸入中模式的匹配結果的下一個出現。我們使用 System.in 類建立了一個掃描器物件。我們使用 findInLine(String) 方法檢查了單詞 World。

package com.tutorialspoint;

import java.util.Scanner;

public class ScannerDemo {
   public static void main(String[] args) {

      // create a new scanner with the System input
      Scanner scanner = new Scanner(System.in);

      // find a string "World"
      System.out.println(scanner.findInLine("World"));

      // print the next line of the string
      System.out.println(scanner.nextLine());

      // close the scanner
      scanner.close();
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:(我們輸入 Hello World 並按回車鍵。)

Hello World
World
java_util_scanner.htm
廣告
© . All rights reserved.