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