我的应用程序中有一个(大)字符串文件,其中包含一系列随机字符 [a-Z] 和 [0-9],还有“;”、“/”、“?”、“:”和“@” ”。我希望我的应用程序能够告诉我连续显示 6 位数字的最近位置(例如“105487”或“558463”)。
实现此目标的最佳方法是什么? 感谢您调查此事。
最佳答案
你可以使用正则表达式。
String regex = "(\\d{6})";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(YOU STRING HERE);
// Check all occurrences
while (matcher.find()) {
System.out.print("Start index: " + matcher.start());
System.out.print(" End index: " + matcher.end());
System.out.println(" Found: " + matcher.group());
}
这样就可以了。
(来自 here 的代码示例)
关于Java - 如何在字符串中搜索 6 个随机数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12681275/