Java 字符串截取:从'rule 0 deny ip destination 30.18.74.0 0.0.0.255'中提取关键信息
你可以使用Java的字符串截取方法substring()来实现这个需求。
String rule = 'rule 0 deny ip destination 30.18.74.0 0.0.0.255';
// 提取0
int startIndex = rule.indexOf('rule ') + 5;
int endIndex = rule.indexOf(' ', startIndex);
String ruleNumber = rule.substring(startIndex, endIndex);
// 提取30.18.74.0
startIndex = rule.indexOf('destination ') + 12;
endIndex = rule.indexOf(' ', startIndex);
String ip = rule.substring(startIndex, endIndex);
// 提取0.0.0.255
startIndex = endIndex + 1;
String mask = rule.substring(startIndex);
System.out.println('ruleNumber: ' + ruleNumber);
System.out.println('ip: ' + ip);
System.out.println('mask: ' + mask);
输出结果:
ruleNumber: 0
ip: 30.18.74.0
mask: 0.0.0.255
原文地址: https://www.cveoy.top/t/topic/b25e 著作权归作者所有。请勿转载和采集!