栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Java Scanner中使用定界符?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Java Scanner中使用定界符?

扫描仪还可以使用空格以外的定界符。

来自Scanner API的简单示例:

 String input = "1 fish 2 fish red fish blue fish"; // \s* means 0 or more repetitions of any whitespace character  // fish is the pattern to find Scanner s = new Scanner(input).useDelimiter("\s*fish\s*"); System.out.println(s.nextInt());   // prints: 1 System.out.println(s.nextInt());   // prints: 2 System.out.println(s.next());      // prints: red System.out.println(s.next());      // prints: blue // don't forget to close the scanner!! s.close(); 

关键是要了解内的正则表达式

(regex)Scanner::useDelimiter
。在此处找到
useDelimiter
教程。

要从正则表达式开始,你可以在这里找到一个不错的教程。

笔记

abc…    Letters123…    Digitsd      Any DigitD      Any Non-digit character.       Any Character.      Period[abc]   only a, b, or c[^abc]  Not a, b, nor c[a-z]   Characters a to z[0-9]   Numbers 0 to 9w      Any Alphanumeric characterW      Any Non-alphanumeric character{m}     m Repetitions{m,n}   m to n Repetitions*       Zero or more repetitions+       One or more repetitions?       Optional characters      Any WhitespaceS      Any Non-whitespace character^…$     Starts and ends(…)     Capture Group(a(bc)) Capture Sub-group(.*)    Capture all(ab|cd) Matches ab or cd


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/402363.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号