我必须编写一个模拟器来读取所有格式完全相同的字符串行。它们看起来像“添加长度为10的Job_A”,唯一的变化是第二个单词和末尾的数字。如何访问并将Job_A和10和另存为变量。我无法计算字符串字母,因为作业的名称长度可能不同,而整数的位数可能不同?
您可以使用正则表达式,例如:
String input = "Add Job_A with length 10";
Pattern pattern = Pattern.compile("Add (.+) with length (\\d+)");
Matcher matcher = pattern.matcher(input);
if (matcher.matches()) {
// Input has correct format, we can extract matched values into variables
String jobName = matcher.group(1);
int number = Integer.parseInt(matcher.group(2));
} else {
// Input has incorrect format
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句