Java Scanner findInLine()方法与示例

news/2024/6/3 21:36:09 标签: 字符串, java, python, 正则表达式, 设计模式

扫描仪类findInLine()方法 (Scanner Class findInLine() method)

Syntax:

句法:

    public String findInLine(Pattern patt);
    public String findInLine(String patt);

  • findInLine() method is available in java.util package.

    findInLine()方法java.util包中可用。

  • findInLine(Pattern patt) method is used to get the string that meets the given pattern (Pattern).

    findInLine(Pattern patt)方法用于获取符合给定模式(Pattern)的字符串

  • findInLine(String patt) method is used to get the string that meets the given String (patt).

    findInLine(String patt)方法用于获取符合给定String(patt)的字符串

  • These methods may throw an exception at the time of returning pattern.

    这些方法在返回模式时可能会引发异常。

    IllegalStateException: This exception may throw when this Scanner is not opened.

    IllegalStateException :如果未打开此扫描器,则可能引发此异常。

  • These are non-static methods, it is accessible with class object & if we try to access these methods with the class name then we will get an error.

    这些是非静态方法,可通过类对象访问;如果尝试使用类名称访问这些方法,则会收到错误消息。

Parameter(s):

参数:

  • In the first case, findInLine(Pattern patt),

    在第一种情况下, findInLine(Pattern patt)

    • Pattern patt – represents the pattern to find for.
    • 模式patt –表示要查找的模式。
  • In the second case, findInLine(String patt),

    在第二种情况下, findInLine(String patt)

    • String patt – represents the string denotes the pattern to find for.
    • 字符串patt –表示字符串,表示要查找的模式。

Return value:

返回值:

In both the cases, the return type of the method is String, it returns the message that meets the given pattern.

在这两种情况下,方法的返回类型均为String ,它返回符合给定模式的消息。

Example:

例:

// Java program is to demonstrate the
// example of findInLine

import java.util.*;
import java.util.*;
import java.util.regex.*;

public class FindInLine {
 public static void main(String[] args) {
  String str = "Java Programming! 3 * 8= 24";

  // Instantiates Scanner
  Scanner sc = new Scanner(str);

  // By using findInLine(Pattern) method
  // is to find the next pattern from the given
  // parameter ".Program"
  String pattern = sc.findInLine(Pattern.compile(".Program"));
  System.out.println("sc.findInLine(Pattern.compile(.Program)): " + pattern);

  // By using findInLine(string) method
  // is to find the next pattern from the given
  // parameter of string "Java"

  pattern = sc.findInLine("ava");
  System.out.println("sc.findInLine(ava): " + pattern);

  // Scanner closed
  sc.close();
 }
}

Output

输出量

sc.findInLine(Pattern.compile(.Program)):  Program
sc.findInLine(ava): null


翻译自: https://www.includehelp.com/java/scanner-findinline-method-with-example.aspx


http://www.niftyadmin.cn/n/1255130.html

相关文章

java valueof_Java Byte类的valueOf()方法及示例

java valueofSyntax: 句法: public static Byte valueOf (byte value);public static Byte valueOf (String value);public static Byte valueOf (String value, int radixs);简短的类valueOf()方法 (Short class valueOf() method) valueOf() method is available…

activemq安装与配置_Windows部署tomcat项目(包括mysql, activemq, redis)

Windows部署tomcat项目(包括mysql, activemq, redis)安装 JDK一、准备工具:1. JDK JDK 可以到官网下载http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html根据系统的版本下载相对应的JDK。我这里选择:jdk…

js 子节点父节点兄弟节点_打印所有没有兄弟的节点

js 子节点父节点兄弟节点Problem Statement: 问题陈述: Given a Binary Tree write a program to print the nodes which don’t have a sibling node. Print all the nodes separated by space which dont have sibling in the tree in sorted order if every nod…

顺序队列 循环队列 入队 出队 链式队列 双端队列 队列应用 C语言实现

队列 顺序实现 #define MaxSize 10 typedef struct{ElemType data[MaxSize];//静态数组存放队列元素int front,rear;//队头和队尾指针 }SqQueue;循环队列——入队 bool EnQueue(SqQueue &Q,ElemType x){if((Q.rear1)%MaxSizeQ.front){//队满return false;}Q.data[Q.rear…

java 根据类名示例化类_Java即时类| atZone()方法与示例

java 根据类名示例化类即时类atZone()方法 (Instant Class atZone() method) atZone() method is available in java.time package. atZone()方法在java.time包中可用。 atZone() method is used to return ZonedDateTime to merge this Instant with the given zone. atZone()…

chrome表单自动填充去掉input黄色背景解决方案 input:-webkit-autofill 禁用

在使用chrome浏览器设计网页时,想将input背景改成透明,也就是 background-color:transparent; 可是效果并不如人意 hack方法:【测试可以使用】 input:-webkit-autofill,textarea:-webkit-autofill,select:-webkit-autofill{-webkit-box-shado…

python办公自动化excel_最全总结 | 聊聊 Python 办公自动化之 Excel(下)

1. 前言 前面谈到 Python 处理 Excel 文件最常见的两种方式,即:xlrd/xlwt、openpyxl ​其中, xlrd/xlwt 这一组合,xlrd 可以负责读取数据,而 xlwt 则负责写入数据,缺点是不支持 xlsx openpyxl 同时支持对 E…

矩阵压缩存储 对称矩阵 三角矩阵 三对角矩阵 稀疏矩阵

对称矩阵 n阶矩阵中任意一个元素aij都有aijaji,则为对称矩阵 只存储主对角线下三角区(按行优先存储在一维数组中) 数组大小:(1n)*n/2 三角矩阵 三对角矩阵 稀疏矩阵 三元表(行,列,值&#xff0…