0

So I have a filename that looks like this:

myFile.12345.txt

If I wanted to end up with just the "12345" how would I go about removing that from the filename if the 12345 could be anywhere between 1 and 5 numbers in length?

4

4 回答 4

6

.如果你确定肯定会有 2 个时期

String fileName = string.split("\\.")[1]
于 2012-10-11T20:23:32.647 回答
3

你可以用这个

String s="ghgj.7657676.jklj";
String p = s.substring(s.indexOf(".")+1,s.lastIndexOf("."));
于 2012-10-11T20:44:18.957 回答
2

假设您要提取所有数字,您可以使用简单的正则表达式来删除所有非数字字符:

String s = "myFile.12345.txt";
String numbers = s.replaceAll("[^\\d]","");
System.out.println(numbers); //12345

注意:它不适file12.12345.txt用于例如

于 2012-10-11T20:23:47.917 回答
2
static final Pattern P = Pattern.compile("^(.*?)\\.(.*?)\\.(.*?)$");
...
...
...
Matcher m = P.matcher(input);
if (m.matches()) {
  //String first = m.group(1);
  String middle = m.group(2);
  //String last = m.group(3);
  ...
}
于 2012-10-11T20:24:48.007 回答