[英]Parsing string from the name
我试图从文件名解析某个名称。
文件名的示例是
xs_1234323_00_32
sf_12345233_99_12
fs_01923122_12_12
我使用String parsedname= child.getName().substring(4.9)
来获取第一行中的1234323。 相反,我如何格式化以上3以仅输出中间数字(在两个_之间)? 什么用split
?
一线解决方案
String n = str.replaceAll("\\D+(\\d+).+", "$1");
最有效的解决方案
int i = str.indexOf('_');
int j = str.indexOf('_', i + 1);
String n = str.substring(i + 1, j);
String [] tokens = filename.split("_");
/* xs_1234323_00_32 would be
[0]=>xs [1]=> 1234323 [2]=> 00 [3] => 32
*/
String middleNumber = tokens[2];
您可以尝试使用'_'分隔符进行拆分。
String.split方法将此字符串拆分为给定;参数的匹配。 所以像这样使用
String[] output = input.split("_");
这里output[1]
将是您想要的结果
和输入将是一样的
String input = "xs_1234323_00_32"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.