findInLine(Pattern pattern)
java.util.Scanner类的findInLine(Pattern pattern)方法尝试忽略分隔符来查找指定模式的下一个匹配项。如果在下一个行分隔符之前找到该模式,则扫描程序将前进经过匹配的输入并返回与该模式匹配的字符串。如果直到下一个行分隔符的输入中都未检测到这种模式,则返回null,并且扫描仪的位置不变。
用法:
public String findInLine(Pattern pattern)
参数:该函数接受强制参数“图案图案”,即要扫描的图案。
返回值:该函数返回扫描仪的定界图案。
异常:如果关闭此扫描器,则此函数将引发IllegalStateException。
以下示例程序旨在说明上述函数:
示例1:
// Java program to illustrate the
// findInLine() method of Scanner class in Java
import java.util.*;
import java.util.regex.Pattern;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
// Get the string to be searched
String s = "Geeksforgeeks has Scanner Class Methods";
// Print the string
System.out.println("Target String:\n" + s);
// create a new scanner
// with the specified String Object
Scanner scanner = new Scanner(s);
// finds a pattern of any 5 letter plus "for"
System.out.println("\nAny 5 letter plus for : "
+ scanner.findInLine(
Pattern.compile(".....for")));
// close the scanner
scanner.close();
}
catch (IllegalStateException e) {
System.out.println("Exception thrown : " + e);
}
}
}
Target String: Geeksforgeeks has Scanner Class Methods Any 5 letter plus for : Geeksfor
示例2:演示IllegalStateException
// Java program to illustrate the
// findInLine() method of Scanner class in Java
import java.util.*;
import java.util.regex.Pattern;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
// Get the string to be searched
String s = "Geeksforgeeks has Scanner Class Methods";
// Print the string
System.out.println("Target String:\n" + s);
// create a new scanner
// with the specified String Object
Scanner scanner = new Scanner(s);
// close the scanner
scanner.close();
// finds a pattern of any 5 letter plus "for"
System.out.println("\nAny 5 letter plus for : "
+ scanner.findInLine(
Pattern.compile(".....for")));
// print the next line of the string
System.out.println("" + scanner.nextLine());
}
catch (IllegalStateException e) {
System.out.println("Exception thrown: " + e);
}
}
}
Target String: Geeksforgeeks has Scanner Class Methods Exception thrown: java.lang.IllegalStateException: Scanner closed
findInLine(String pattern)
java.util.Scanner类的findInLine(String pattern)方法尝试查找由指定的字符串模式构造的模式的下一次出现,而忽略了分隔符。
用法:
public String findInLine(String pattern)
参数:该函数接受扫描的强制性参数字符串模式。
返回值:该函数返回扫描仪的定界图案。
异常:如果关闭此扫描器,则此函数将引发IllegalStateException。
以下示例程序旨在说明上述函数:
示例1:
// Java program to illustrate the
// findInLine() method of Scanner class in Java
import java.util.*;
import java.util.regex.Pattern;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
// Get the string to be searched
String s = "Geeksforgeeks has Scanner Class Methods";
// Print the string
System.out.println("Target String:\n" + s);
// create a new scanner
// with the specified String Object
Scanner scanner = new Scanner(s);
// finds a pattern of any 5 letter plus "for"
System.out.println("\nAny 5 letter plus for : "
+ scanner.findInLine(
Pattern.compile(".....for")));
// close the scanner
scanner.close();
}
catch (IllegalStateException e) {
System.out.println("Exception thrown : " + e);
}
}
}
Target String: Geeksforgeeks has Scanner Class Methods Any 5 letter plus for : Geeksfor
示例2:演示IllegalStateException
// Java program to illustrate the
// findInLine() method of Scanner class in Java
import java.util.*;
import java.util.regex.Pattern;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
// Get the string to be searched
String s = "Geeksforgeeks has Scanner Class Methods";
// Print the string
System.out.println("Target String:\n" + s);
// create a new scanner
// with the specified String Object
Scanner scanner = new Scanner(s);
// close the scanner
scanner.close();
// finds a pattern of any 5 letter plus "for"
System.out.println("\nAny 5 letter plus for : "
+ scanner.findInLine(
Pattern.compile(".....for")));
}
catch (IllegalStateException e) {
System.out.println("Exception thrown : " + e);
}
}
}
Target String: Geeksforgeeks has Scanner Class Methods Exception thrown : java.lang.IllegalStateException: Scanner closed
参考: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#findInLine(java.util.regex.Pattern)
相关用法
- Java Scanner nextInt()用法及代码示例
- Java Scanner hasNextInt()用法及代码示例
- Java Scanner hasNextByte()用法及代码示例
- Java Scanner nextByte()用法及代码示例
- Java Scanner hasNextBigInteger()用法及代码示例
- Java Scanner hasNextDouble()用法及代码示例
- Java Scanner hasNextBoolean()用法及代码示例
- Java Scanner hasNextBigDecimal()用法及代码示例
- Java Scanner nextBigInteger()用法及代码示例
- Java Scanner locale()用法及代码示例
- Java Scanner nextLine()用法及代码示例
- Java Scanner ioException()用法及代码示例
- Java Scanner nextBigDecimal()用法及代码示例
- Java Scanner nextBoolean()用法及代码示例
- Java Scanner hasNextShort()用法及代码示例
注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 Scanner findInLine() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。