java usedelimiter_Java Scanner useDelimiter()用法及代码示例

useDelimiter(Pattern pattern)

java.util.Scanner类的useDelimiter(Pattern pattern)方法将此扫描程序的定界模式设置为指定的模式。

用法:

public Scanner useDelimiter(Pattern pattern)

参数:该函数接受一个强制性的参数模式,该模式指定一个定界模式。

返回值:函数返回此扫描仪。

以下示例程序旨在说明上述功能:

示例1:

// Java program to illustrate the useDelimiter()

// 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

{

String s = "Geeksforgeeks has Scanner Class Methods";

// create a new scanner

// with the specified String Object

Scanner scanner = new Scanner(s);

// print a line of the scanner

System.out.println("Scanner String: \n"

+ scanner.nextLine());

// display the old delimiter

System.out.println("Old delimiter: "

+ scanner.delimiter());

// change the delimiter of this scanner

scanner.useDelimiter(Pattern.compile(".ll."));

// display the new delimiter

System.out.println("New delimiter: "

+ scanner.delimiter());

// close the scanner

scanner.close();

}

}

输出:

Scanner String:

Geeksforgeeks has Scanner Class Methods

Old delimiter: \p{javaWhitespace}+

New delimiter: .ll.

useDelimiter(String pattern)

java.util.Scanner类的useDelimiter(String pattern)方法将此扫描仪的定界模式设置为从指定String构造的模式。

用法:

public Scanner useDelimiter(String pattern)

参数:该函数接受强制性的参数字符串模式,该模式指定了定界模式。

返回值:函数返回此扫描仪。

以下示例程序旨在说明上述功能:

示例1:

// Java program to illustrate the useDelimter()

// method of Scanner class in Java

import java.util.*;

public class GFG1 {

public static void main(String[] argv)

throws Exception

{

String s = "Geeksforgeeks has Scanner Class Methods";

// create a new scanner

// with the specified String Object

Scanner scanner = new Scanner(s);

// print a line of the scanner

System.out.println("Scanner String: \n"

+ scanner.nextLine());

// print the old delimiter

System.out.println("Old Delimiter: "

+ scanner.delimiter());

// change the delimiter

scanner.useDelimiter("Wor");

// print the new delimiter

System.out.println("New Delimiter: "

+ scanner.delimiter());

// close the scanner

scanner.close();

}

}

输出:

Scanner String:

Geeksforgeeks has Scanner Class Methods

Old Delimiter: \p{javaWhitespace}+

New Delimiter: Wor

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值