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


Java Java.util.Scanner.findInLine()用法及代碼示例


描述

這個java.util.Scanner.findInLine(Pattern pattern)方法嘗試查找指定模式的下一次出現,忽略分隔符。如果在下一行分隔符之前找到該模式,則掃描器前進到匹配的輸入並返回與該模式匹配的字符串。如果在下一行分隔符之前的輸入中沒有檢測到這樣的模式,則返回 null 並且掃描儀的位置不變。此方法可能會阻止等待與模式匹配的輸入。

聲明

以下是聲明java.util.Scanner.findInLine()方法

public String findInLine(Pattern pattern)

參數

pattern- 要掃描的模式

返回值

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

異常

IllegalStateException- 如果此掃描儀已關閉

示例

下麵的例子展示了 java.util.Scanner.findInLine() 方法的用法。

package com.tutorialspoint;

import java.util.*;
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

相關用法


注:本文由純淨天空篩選整理自 Java.util.Scanner.findInLine() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。